C# 在现有 XML 中插入新的子节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15044243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert new child node in an existing XML
提问by Ju-chan
Good day everyone. I would like to ask for help with my code. I have here an XML document containing the following.
今天是个好日子。我想就我的代码寻求帮助。我这里有一个包含以下内容的 XML 文档。
<?xml version="1.0" encoding="utf-8" ?>
<TechnicalReport>
<Data quantity = "2" description ="myDesc" findings = "none" actiontaken = "none" />
</TechnicalReport>
What I would like to do here is to add another child node inside the . I have searched for so many websites about my problem but to no avail. For example, I will add another node, say:
我想在这里做的是在 . 我已经搜索了很多关于我的问题的网站,但无济于事。例如,我将添加另一个节点,例如:
<?xml version="1.0" encoding="utf-8" ?>
<TechnicalReport>
<Data quantity = "2" description ="myDesc" findings = "none" actiontaken = "none" />
<Data quantity = "3" description ="myDesc2" findings = "none2" actiontaken = "none3" />
</TechnicalReport>
I have successfully compiled and loaded the XML file into a Repeater control using an XMLDataSource, but when I do an insert from my form, the Repeater control does not update its contents, and even my XML file also does not update.
我已经使用 XMLDataSource 成功地将 XML 文件编译并加载到 Repeater 控件中,但是当我从表单中插入时,Repeater 控件不会更新其内容,甚至我的 XML 文件也不会更新。
Here's my C# code:
这是我的 C# 代码:
public void AddNewRecord()
{
//Load XML Schema
XmlDocument originalXml = new XmlDocument();
originalXml.Load(Server.MapPath("xmlTechReportDetails.xml"));
//Create the node name Technical Report
XmlNode TechReport = originalXml.SelectSingleNode("TechnicalReport");
XmlNode Data = originalXml.CreateNode(XmlNodeType.Element, "Data", null);
//Insert quantity
XmlAttribute quantity = originalXml.CreateAttribute("quantity");
quantity.Value = txtQty.Text;
//Insert description
XmlAttribute description = originalXml.CreateAttribute("description");
description.Value = txtDescription.Text;
//Insert findings
XmlAttribute findings = originalXml.CreateAttribute("findings");
findings.Value = txtFindings.Text;
//Insert actions taken.
XmlAttribute actionTaken = originalXml.CreateAttribute("actiontaken");
actionTaken.Value = txtAction.Text;
Data.Attributes.Append(quantity);
Data.Attributes.Append(description);
Data.Attributes.Append(findings);
Data.Attributes.Append(actionTaken);
TechReport.AppendChild(Data);
}
Please help.
请帮忙。
采纳答案by klce
Try adding this at the end of your method:
尝试在您的方法末尾添加此内容:
originalXml.Save(Server.MapPath("xmlTechReportDetails.xml"));
I think it's because you did not save the file. That's why your changes are not retained.
我认为这是因为您没有保存文件。这就是不保留您的更改的原因。
回答by Sangram Chougale
Instead of this code:
而不是这个代码:
//Create the node name Technical Report
XmlNode TechReport = originalXml.SelectSingleNode("TechnicalReport");
Use this code
使用此代码
XmlNodeList nodeList = originalXml.GetElementsByTagName("connectionStrings");