使用System.Xml

时间:2015-09-11 10:33:17

标签: c# .net xml


我正在尝试替换XML文件中的节点,但我需要一些帮助来解决如何用非唯一名称或标识符替换节点。

问题是,如何使用XmlDataReplace中的数据替换以下节点:

/Document/ExclusionContainer/Mobile_Devices

以下是示例代码中使用的一些示例代码和数据。

示例代码

private void ReplaceXmlArray()
{
 System.Xml.XmlDocument OriginalXmlDoc = new System.Xml.XmlDocument();
 //Scroll Down for XmlData     
 OriginalXmlDoc.LoadXml("StackOverflow - XmlDataOriginal");      
 string NewXmlContent = "StackOverFlow - XmlDataReplace";

 //Need help here on Code to Replace
 //Node in OriginalXmlDoc
}

XmlDataOriginal - 原始文档

<Document>

 <DefaultContainer>

  <Mobile_Devices>
   <Mobile_Device>
    <Id>1</Id>
    <Name>Device-One</Name>
   </Mobile_Device>
  </Mobile_Devices>

 </DefaultContainer> 

 <ExlusionContainer>

  <Mobile_Devices>
   <Mobile_Device>
    <Id>2</Id>
    <Name>Device-Two</Name>
   </Mobile_Device>
  </Mobile_Devices>

  <Laptops />

 </ExclusionContainer>

</Document>

XmlDataReplace - 将替换XmlData1中的数据(Document / ExclusionContainer /)

<Mobile_Devices>
   <Mobile_Device>
    <Id>2</Id>
    <Name>Device-Two</Name>
   </Mobile_Device>
   <Mobile_Device>
    <Id>3</Id>
    <Name>Device-Three</Name>
   </Mobile_Device>
</Mobile_Devices>

2 个答案:

答案 0 :(得分:1)

明白你的意思。 如果我错了,请纠正我: - 您想要更新(替换或创建)数据。 让我们这样做 我选择这两个xmls

Orignal.xml存储在c:\ data \ orignal.xml

<?xml version="1.0" encoding="utf-8"?>
<Document>
  <DefaultContainer>
    <Mobile_Devices>
      <Mobile_Device>
        <Id>1</Id>
        <Name>Device-One</Name>
      </Mobile_Device>
    </Mobile_Devices>
  </DefaultContainer>
  <ExlusionContainer>
    <Mobile_Devices>
      <Mobile_Device>
        <Id>2</Id>
        <Name>Device-Two</Name>
      </Mobile_Device>
    </Mobile_Devices>
  </ExlusionContainer>
</Document>

存储在c:\ data \ replace.xml

中的第二个replace.xml
<?xml version="1.0" encoding="utf-8" ?>
<ExclusionContainer>
  <Mobile_Devices>
    <Mobile_Device>
      <Id>2</Id>
      <Name>Device-Two</Name>
    </Mobile_Device>
  </Mobile_Devices>
  <Mobile_Devices>
    <Mobile_Device>
      <Id>3</Id>
      <Name>Device-Three</Name>
    </Mobile_Device>
  </Mobile_Devices>
  <Laptops />
  </ExclusionContainer>

复制粘贴以下代码

     XmlDocument originalDoc = new XmlDocument();
        originalDoc.Load("c:\\data\\Orignal.xml");
        XmlNode exclusionNode = originalDoc.SelectSingleNode("/Document/ExlusionContainer");

        XmlDocument docToReplace = new XmlDocument();
        docToReplace.Load("c:\\data\\replace.xml");

        XmlNode replaceNode = docToReplace.SelectSingleNode("/ExclusionContainer");
        exclusionNode.InnerXml = replaceNode.InnerXml;
        originalDoc.Save("c:\\data\\orignal.xml");

答案 1 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 

                "<Document>" +
                  "<DefaultContainer>" +
                    "<Mobile_Devices>" +
                      "<Mobile_Device>" +
                        "<Id>1</Id>" +
                        "<Name>Device-One</Name>" +
                      "</Mobile_Device>" +
                    "</Mobile_Devices>" +
                  "</DefaultContainer>" +
                  "<ExlusionContainer>" +
                    "<Mobile_Devices>" +
                      "<Mobile_Device>" +
                        "<Id>2</Id>" +
                        "<Name>Device-Two</Name>" +
                      "</Mobile_Device>" +
                    "</Mobile_Devices>" +
                    "<Laptops />" +
                  "</ExlusionContainer>" +
                "</Document>";

            string newNode = 
                "<Mobile_Device>" +
                    "<Id>3</Id>" +
                    "<Name>Device-Three</Name>" +
                   "</Mobile_Device>";



            XDocument doc = XDocument.Parse(input);
            XElement newElement = XElement.Parse(newNode);

            XElement exlusionContainer = doc.Descendants("ExlusionContainer").FirstOrDefault();
            XElement mobileDevice = exlusionContainer.Element("Mobile_Devices").Element("Mobile_Device");
            mobileDevice.ReplaceWith(new XElement[] { mobileDevice, newElement });
        }
    }
}
​