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 に答える