大家好日子。我想请求帮助我的代码。我这里有一个包含以下内容的XML文档。
<?xml version="1.0" encoding="utf-8" ?>
<TechnicalReport>
<Data quantity = "2" description ="myDesc" findings = "none" actiontaken = "none" />
</TechnicalReport>
我想在这里做的是在里面添加另一个子节点。我搜索了很多关于我的问题的网站,但无济于事。例如,我将添加另一个节点,例如:
<?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>
我已经使用XMLDataSource成功编译并将XML文件加载到Repeater控件中,但是当我从表单执行插入时,Repeater控件不会更新其内容,甚至我的XML文件也不会更新。
这是我的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);
}
请帮忙。
答案 0 :(得分:4)
尝试在方法结束时添加此内容:
originalXml.Save(Server.MapPath("xmlTechReportDetails.xml"));
我认为这是因为你没有保存文件。这就是为什么不保留您的更改的原因。
答案 1 :(得分:0)
而不是这段代码:
//Create the node name Technical Report
XmlNode TechReport = originalXml.SelectSingleNode("TechnicalReport");
使用此代码
XmlNodeList nodeList = originalXml.GetElementsByTagName("connectionStrings");