将新元素添加到XML文件 [英] Add a new element to an XML file
本文介绍了将新元素添加到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友,我有一个XML文件
<? xml 版本 =" 编码 =" utf-8" ? < HEDDERS >
< HEDDER >
< 名称 > AAAAA < /名称 >
< /HEDDER >
< HEDDER >
< 名称 > BBBBB < /名称 >
< /HEDDER >
< HEDDER >
< 名称 > CCCCC < /名称 >
< /HEDDER >
< HEDDER >
< 名称 > DDDDD < /名称 >
< /HEDDER >
< HEDDER >
< 名称 > EEEEE < /名称 >
< /HEDDER >
< /HEDDERS > pre>
我想添加一个新元素
< HEDDER > ;
< 名称 > FFFFFF < /名称 >
< /HEDDER >
最后一个< HEADDER>之后元素.
使用ASP.Net C#
解决方案
您可以使用 ^ ]将新的孩子添加到HEDDERS.请参阅MSDN文档中的小示例.
以下代码可用于添加元素
字符串 xmlText = @" <?xml version =" 1.0" encoding =" utf-8"?>
< HEDDERS>
< HEDDER>
<名称> AAAAA</名称>
</HEDDER>
< HEDDER>
<名称> BBBBB</名称>
</HEDDER>
< HEDDER>
<名称> CCCCC</名称>
</HEDDER>
< HEDDER>
<名称> DDDDD</名称>
</HEDDER>
< HEDDER>
<名称> EEEEE</名称>
</HEDDER>
</HEDDERS>";
XElement元素= XElement.Parse(xmlText);
element.Add(新 XElement(" 跨度>,
新 XElement(" ," )
));
// < HEDDERS>
// < HEDDER>
// <名称> AAAAA</名称>
// </HEDDER>
// < HEDDER>
// <名称> BBBBB</名称>
// </HEDDER>
// < HEDDER>
// <名称> CCCCC</名称>
// </HEDDER>
// < HEDDER>
// <名称> DDDDD</名称>
// </HEDDER>
// < HEDDER>
// <名称> EEEEE</名称>
// </HEDDER>
// < HEDDER>
// <名称> FFFFFF</名称>
// </HEDDER>
// </HEDDERS>
我写这样的代码
XDocument doc;
// 验证文件是否存在
如果(!System.IO.File.Exists(file))
{
doc = 新 XDocument(新 XDeclaration(" 1.0", UTF-8"," ),
新 System.Xml.Linq.XElement(" ));
}
其他
{
doc = XDocument.Load(file);
XElement ele = 新 XElement(" ,TextBox1.Text);
doc.Root.Element(" ).Add(ele);
doc.Save(文件);
}
但这显示了一个错误. 对象引用未设置为对象的实例."
hi friends, I have an XML file
<?xml version="1.0" encoding="utf-8" ?>
<HEDDERS>
<HEDDER>
<Name>AAAAA</Name>
</HEDDER>
<HEDDER>
<Name>BBBBB</Name>
</HEDDER>
<HEDDER>
<Name>CCCCC</Name>
</HEDDER>
<HEDDER>
<Name>DDDDD</Name>
</HEDDER>
<HEDDER>
<Name>EEEEE</Name>
</HEDDER>
</HEDDERS>
I want to add a new element
<HEDDER>
<Name>FFFFFF</Name>
</HEDDER>
after the last <HEADDER> element.
using ASP.Net C#
解决方案 You can use the AppendChild[^] to add a new child to HEDDERS. Please see the small example in the MSDN documentation.
The following code can be used to add the element
string xmlText = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<HEDDERS>
<HEDDER>
<Name>AAAAA</Name>
</HEDDER>
<HEDDER>
<Name>BBBBB</Name>
</HEDDER>
<HEDDER>
<Name>CCCCC</Name>
</HEDDER>
<HEDDER>
<Name>DDDDD</Name>
</HEDDER>
<HEDDER>
<Name>EEEEE</Name>
</HEDDER>
</HEDDERS>";
XElement element = XElement.Parse(xmlText);
element.Add(new XElement("HEDDER",
new XElement("Name","FFFFFF")
));
//<HEDDERS>
// <HEDDER>
// <Name>AAAAA</Name>
// </HEDDER>
// <HEDDER>
// <Name>BBBBB</Name>
// </HEDDER>
// <HEDDER>
// <Name>CCCCC</Name>
// </HEDDER>
// <HEDDER>
// <Name>DDDDD</Name>
// </HEDDER>
// <HEDDER>
// <Name>EEEEE</Name>
// </HEDDER>
// <HEDDER>
// <Name>FFFFFF</Name>
// </HEDDER>
//</HEDDERS>
I write code like this
XDocument doc;
//Verify whether a file is exists or not
if (!System.IO.File.Exists(file))
{
doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
new System.Xml.Linq.XElement("HEADDERS"));
}
else
{
doc = XDocument.Load(file);
XElement ele = new XElement("Name", TextBox1.Text);
doc.Root.Element("HEADDER").Add(ele);
doc.Save(file);
}
But this shows an error. " Object reference not set to an instance of an object."
这篇关于将新元素添加到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文