xmlelement怎么添加节点
1个回答
展开全部
首先获取想要复制节点值添加
XElement rootNode = XElement.Load("test.xml");
var result = from m in rootNode.Elements ("MM02")
where m.Attribute ("id").Value =="1"
select new
{
id=m.Attribute ("id").Value ,
fname = m.Element ("FNAM").Value ,
fval = m.Element("FVAL").Value
};
foreach (var s in result)
{
XElement x =
new XElement("MM02", new XAttribute("id",s.id ),
new XElement ("FNAM",s.fname ),
new XElement ("FVAL",s.fval)
);
XElement node = rootNode.Element("MM02");
rootNode.Element("MM02").AddAfterSelf(x);
rootNode.Save("test.xml");
}
XElement rootNode = XElement.Load("test.xml");
var result = from m in rootNode.Elements ("MM02")
where m.Attribute ("id").Value =="1"
select new
{
id=m.Attribute ("id").Value ,
fname = m.Element ("FNAM").Value ,
fval = m.Element("FVAL").Value
};
foreach (var s in result)
{
XElement x =
new XElement("MM02", new XAttribute("id",s.id ),
new XElement ("FNAM",s.fname ),
new XElement ("FVAL",s.fval)
);
XElement node = rootNode.Element("MM02");
rootNode.Element("MM02").AddAfterSelf(x);
rootNode.Save("test.xml");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询