【问题标题】:how do i create and insert a node inside another node如何在另一个节点中创建和插入节点
【发布时间】:2015-04-18 14:56:23
【问题描述】:

我正在尝试在 C# 中的现有 xmldocument 中插入一个带有另一个子 xml 节点的 xml 节点。 我有一个看起来像这样的 XML 文档:

<?xml version="1.0" encoding="utf-16"?>
<DictionarySerializer>
    <item>
        <key>statusCode</key>
        <value>0</value>
    </item>
    <item>
        <key>statusSeverity</key>
        <value>INFO</value>
    </item>
    <item>
        <key>statusMessage</key>
        <value>Status OK</value>
    </item>
    <item>
        <key>MerchantAccountNumber</key>
        <value>9999999999</value>
    </item>
    <item>
        <key>ReconBatchID</key>
        <value>420150418 1Q02144266965047801046AUTO04</value>
    </item>
    <item>
        <key>PaymentGroupingCode</key>
        <value>3</value>
    </item>
    <item>
        <key>responsePaymentStatus</key>
        <value>Completed</value>
    </item>
    <item>
        <key>TxnAuthorizationTime</key>
        <value>2015-04-18T09:14:41</value>
    </item>
    <item>
        <key>TxnAuthorizationStamp</key>
        <value>1429348481</value>
    </item>
    <item>
        <key>ClientTransID</key>
        <value>aidjl79f</value>
    </item>
</DictionarySerializer>

我需要在底部插入另一个节点和节点。 到目前为止我有这个:

  XmlDocument xmlCustomValues = new XmlDocument();
            xmlCustomValues.LoadXml(OldCustomValues);
            XmlNode NodeItem = xmlCustomValues.SelectSingleNode("DictionarySerializer");
            XmlNode NodeNewItem = xmlCustomValues.CreateNode(XmlNodeType.Element, "item", null);
    XmlNode NodeNewKey = NodeNewItem.??????

但不确定如何在 NodeNewItem 下创建节点(没有“CreateNode”方法)。以前从未这样做过(显然),语法对我来说没有意义。


这是一个有效的答案(上面 XML 文档的测试代码)

  string OldCustomValues = this.txtInput.Text;
    XmlDocument xmlCustomValues = new XmlDocument();
    xmlCustomValues.LoadXml(OldCustomValues);
    XmlNode NodeItem = xmlCustomValues.SelectSingleNode("DictionarySerializer");
    XmlNode NodeNewItem = xmlCustomValues.CreateNode(XmlNodeType.Element, "item", null);
    NodeItem.AppendChild(NodeNewItem);
    XmlNode NodeNewKey = xmlCustomValues.CreateNode(XmlNodeType.Element, "key", null);
    NodeNewKey.InnerText = "MyKey";
    XmlNode NodeNewValue = xmlCustomValues.CreateNode(XmlNodeType.Element, "value", null);
    NodeNewValue.InnerText = "MyValue";
    NodeNewItem.AppendChild(NodeNewKey);
    NodeNewItem.AppendChild(NodeNewValue);

    this.txtOutput.Text = xmlCustomValues.OuterXml;

【问题讨论】:

  • 这对你有用吗?
  • 喜欢这个? NodeItem.AppendChild(NodeNewItem); XmlNode NodeNewKey = xmlCustomValues.CreateNode(XmlNodeType.Element, "key", null); NodeNewKey.InnerText = "MyKey"; XmlNode NodeNewValue = xmlCustomValues.CreateNode(XmlNodeType.Element, "value", null); NodeNewValue.InnerText = "我的值"; NodeNewItem.AppendChild(NodeNewKey); NodeNewItem.AppendChild(NodeNewValue);
  • 抱歉,在 StackExchange 上 cmets 部分确实需要更好(无法格式化!!!)
  • 看起来不错,结果如何?
  • 我添加了:this.txtOutput.Text = xmlCustomValues.OuterXml;转换回字符串(测试项目)。成功了!

标签: c# xml xmldocument


【解决方案1】:

你已经创建了节点,你只需要将节点追加到根目录

NodeItem.AppendChild(NodeNewItem);

【讨论】:

    猜你喜欢
    • 2019-06-11
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多