4

今天是个好日子。我想就我的代码寻求帮助。我这里有一个包含以下内容的 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);
}

请帮忙。

4

2 回答 2

4

尝试在方法的末尾添加:

originalXml.Save(Server.MapPath("xmlTechReportDetails.xml"));

我认为这是因为您没有保存文件。这就是不保留您的更改的原因。

于 2013-02-26T10:25:08.247 回答
0

而不是这段代码:

//Create the node name Technical Report
XmlNode TechReport = originalXml.SelectSingleNode("TechnicalReport");

使用此代码

XmlNodeList nodeList = originalXml.GetElementsByTagName("connectionStrings");
于 2015-05-20T12:45:35.473 回答