码迷,mamicode.com
首页 > Windows程序 > 详细

C#操作xml文件

时间:2017-06-19 19:41:35      阅读:25      评论:0      收藏:0      [点我收藏+]

标签:pac   article   oar   detail   进一步   namespace   app   string   好的   

有些时候我们需要生成一个xml文档作为数据交换的容器。当然我们用拼接字符串的方法来进行构建xml,但是这种方法虽然简单有效,但是如果xml文档结构过于复杂,拼接字符串会让人眼花缭乱。这时候就需要C#给我们提供现成的类库,以供我们自由的创建xml文档。

比如我们要创建如下的xml文档

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ns0:Z_AVS_UPLOAD_WEIGHT_Request xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003">     
  3.     <ZWERKS>ZFM1</ZWERKS>  
  4.     <TAB1>  
  5.         <ZNO>13022101</ZNO>  
  6.         <ZORDER>2013238955</ZORDER>  
  7.         <ZWEIGHT>4140</ZWEIGHT>  
  8.     </TAB1>  
  9.     <TAB1>  
  10.         <ZNO>13022101</ZNO>  
  11.         <ZORDER>2013239627</ZORDER>  
  12.         <ZWEIGHT>4140</ZWEIGHT>  
  13.     </TAB1>  
  14. </ns0:Z_AVS_UPLOAD_WEIGHT_Request>  
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Z_AVS_UPLOAD_WEIGHT_Request xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003">   
    <ZWERKS>ZFM1</ZWERKS>
    <TAB1>
        <ZNO>13022101</ZNO>
        <ZORDER>2013238955</ZORDER>
        <ZWEIGHT>4140</ZWEIGHT>
    </TAB1>
    <TAB1>
        <ZNO>13022101</ZNO>
        <ZORDER>2013239627</ZORDER>
        <ZWEIGHT>4140</ZWEIGHT>
    </TAB1>
</ns0:Z_AVS_UPLOAD_WEIGHT_Request>

 

选取这样的结构,一方面是因为它来自于论坛某位坛友实际的需求,另一方面它足够简单却有代表性。

下面我将以这个例子用两种方法(XmlDocument和Linq to XML)进行讲解。

1、XmlDocument

 

  1. XmlDocument document = new XmlDocument();  
  2.   
  3. XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分  
  4. document.AppendChild(declaration);  
  5.   
  6. XmlElement root = document.CreateElement("ns0", "Z_AVS_UPLOAD_WEIGHT_Request", "http://schemas.microsoft.com/BizTalk/2003");  
  7. document.AppendChild(root);  
  8.   
  9. XmlElement zwerks = document.CreateElement("ZWERKS");  
  10. zwerks.InnerText = "ZFM1";  
  11. root.AppendChild(zwerks);  
  12.   
  13. XmlElement tab1 = document.CreateElement("TAB1");  
  14. root.AppendChild(tab1);  
  15.   
  16. XmlElement zno = document.CreateElement("ZNO");  
  17. zno.InnerText = "13022101";  
  18. tab1.AppendChild(zno);  
  19.   
  20. XmlElement zorder = document.CreateElement("ZORDER");  
  21. zorder.InnerText = "2013238955";  
  22. tab1.AppendChild(zorder);  
  23.   
  24. XmlElement zweight = document.CreateElement("ZWEIGHT");  
  25. zweight.InnerText = "4140";  
  26. tab1.AppendChild(zweight);  
  27.   
  28. XmlElement tab2 = document.CreateElement("TAB1");  
  29. root.AppendChild(tab2);  
  30.   
  31. XmlElement zno2 = document.CreateElement("ZNO");  
  32. zno2.InnerText = "13022101";  
  33. tab2.AppendChild(zno2);  
  34.   
  35. XmlElement zorder2 = document.CreateElement("ZORDER");  
  36. zorder2.InnerText = "2013238955";  
  37. tab2.AppendChild(zorder2);  
  38.   
  39. XmlElement zweight2 = document.CreateElement("ZWEIGHT");  
  40. zweight2.InnerText = "4140";  
  41. tab2.AppendChild(zweight2);  
  42.   
  43. document.Save("test.xml");//将生成好的xml保存到test.xml文件中  
            XmlDocument document = new XmlDocument();

            XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分
            document.AppendChild(declaration);

            XmlElement root = document.CreateElement("ns0", "Z_AVS_UPLOAD_WEIGHT_Request", "http://schemas.microsoft.com/BizTalk/2003");
            document.AppendChild(root);

            XmlElement zwerks = document.CreateElement("ZWERKS");
            zwerks.InnerText = "ZFM1";
            root.AppendChild(zwerks);

            XmlElement tab1 = document.CreateElement("TAB1");
            root.AppendChild(tab1);

            XmlElement zno = document.CreateElement("ZNO");
            zno.InnerText = "13022101";
            tab1.AppendChild(zno);

            XmlElement zorder = document.CreateElement("ZORDER");
            zorder.InnerText = "2013238955";
            tab1.AppendChild(zorder);

            XmlElement zweight = document.CreateElement("ZWEIGHT");
            zweight.InnerText = "4140";
            tab1.AppendChild(zweight);

            XmlElement tab2 = document.CreateElement("TAB1");
            root.AppendChild(tab2);

            XmlElement zno2 = document.CreateElement("ZNO");
            zno2.InnerText = "13022101";
            tab2.AppendChild(zno2);

            XmlElement zorder2 = document.CreateElement("ZORDER");
            zorder2.InnerText = "2013238955";
            tab2.AppendChild(zorder2);

            XmlElement zweight2 = document.CreateElement("ZWEIGHT");
            zweight2.InnerText = "4140";
            tab2.AppendChild(zweight2);

            document.Save("test.xml");//将生成好的xml保存到test.xml文件中

2、Linq to XML

  1. XDocument document = new XDocument();  
  2. document.Declaration = new XDeclaration("1.0", "UTF-8", "");  
  3.   
  4. XNamespace ns = "http://schemas.microsoft.com/BizTalk/2003";  
  5.   
  6. XElement root = new XElement(ns + "Z_AVS_UPLOAD_WEIGHT_Request",  
  7.     new XAttribute(XNamespace.Xmlns + "ns0", "http://schemas.microsoft.com/BizTalk/2003"));  
  8.   
  9. root.Add(new XElement("ZWERKS", "ZFM1"),  
  10.     new XElement("TAB1",  
  11.         new XElement("ZNO", 13022101),  
  12.         new XElement("ZORDER", 2013238955),  
  13.         new XElement("ZWEIGHT", 4140)),  
  14.     new XElement("TAB1",  
  15.         new XElement("ZNO", 13022101),  
  16.         new XElement("ZORDER", 2013238955),  
  17.         new XElement("ZWEIGHT", 4140))  
  18.         );  
  19.   
  20. document.Add(root);  
  21. document.Save("test.xml");//保存xml到文件  
            XDocument document = new XDocument();
            document.Declaration = new XDeclaration("1.0", "UTF-8", "");

            XNamespace ns = "http://schemas.microsoft.com/BizTalk/2003";

            XElement root = new XElement(ns + "Z_AVS_UPLOAD_WEIGHT_Request",
                new XAttribute(XNamespace.Xmlns + "ns0", "http://schemas.microsoft.com/BizTalk/2003"));

            root.Add(new XElement("ZWERKS", "ZFM1"),
                new XElement("TAB1",
                    new XElement("ZNO", 13022101),
                    new XElement("ZORDER", 2013238955),
                    new XElement("ZWEIGHT", 4140)),
                new XElement("TAB1",
                    new XElement("ZNO", 13022101),
                    new XElement("ZORDER", 2013238955),
                    new XElement("ZWEIGHT", 4140))
                    );

            document.Add(root);
            document.Save("test.xml");//保存xml到文件

可以发现Linq to XML的方法比较简洁,代码量也足够小。当然了XmlDocument的方法可以进一步的简化,这里重点展示一下Linq to XML的魅力。

呵呵,如上只当给需要的朋友提供一下思路和解决方案。关于Linq to xml的更多信息可以参考:XNamespace

—————————————————————————————————————————————————————————

再提供一个例子:

有a.xml:

 

  1. <a>  
  2.     <xingming>  
  3.         <item>  
  4.             <name>张三</name>  
  5.             <code>1</code>  
  6.         </item>  
  7.         <item>  
  8.             <name>李四</name>  
  9.             <code>2</code>  
  10.         </item>  
  11.         <item>  
  12.             <name>王五</name>  
  13.             <code>3</code>  
  14.         </item>  
  15.         <item>  
  16.             <name>赵四</name>  
  17.             <code>4</code>  
  18.         </item>  
  19.         <item>  
  20.             <name>王二麻子</name>  
  21.             <code>5</code>  
  22.         </item>  
  23.     </xingming>  
  24.     <nianji>  
  25.         <item>  
  26.             <name>一年级</name>  
  27.             <banji>一班</banji>  
  28.         </item>  
  29.     </nianji>  
  30. </a>  
<a>
    <xingming>
        <item>
            <name>张三</name>
            <code>1</code>
        </item>
        <item>
            <name>李四</name>
            <code>2</code>
        </item>
        <item>
            <name>王五</name>
            <code>3</code>
        </item>
        <item>
            <name>赵四</name>
            <code>4</code>
        </item>
        <item>
            <name>王二麻子</name>
            <code>5</code>
        </item>
    </xingming>
    <nianji>
        <item>
            <name>一年级</name>
            <banji>一班</banji>
        </item>
    </nianji>
</a>

想转换成b.xml:

 

  1. <b>  
  2.   <xingming>  
  3.     <items>  
  4.       <item>  
  5.         <Xingname1>张三</Xingname1>  
  6.         <Xingcode1>1</Xingcode1>  
  7.         <Xingname2>李四</Xingname2>  
  8.         <Xingcode2>2</Xingcode2>  
  9.       </item>  
  10.       <item>  
  11.         <Xingname1>王五</Xingname1>  
  12.         <Xingcode1>3</Xingcode1>  
  13.         <Xingname2>赵四</Xingname2>  
  14.         <Xingcode2>4</Xingcode2>  
  15.       </item>  
  16.       <item>  
  17.         <Xingname1>王二麻子</Xingname1>  
  18.         <Xingcode1>5</Xingcode1>  
  19.       </item>  
  20.     </items>  
  21.   </xingming>  
  22.   <nianji>  
  23.     <item>  
  24.       <name>一年级</name>  
  25.       <name>一班</name>  
  26.     </item>  
  27.   </nianji>  
  28. </b>  
<b>
  <xingming>
    <items>
      <item>
        <Xingname1>张三</Xingname1>
        <Xingcode1>1</Xingcode1>
        <Xingname2>李四</Xingname2>
        <Xingcode2>2</Xingcode2>
      </item>
      <item>
        <Xingname1>王五</Xingname1>
        <Xingcode1>3</Xingcode1>
        <Xingname2>赵四</Xingname2>
        <Xingcode2>4</Xingcode2>
      </item>
      <item>
        <Xingname1>王二麻子</Xingname1>
        <Xingcode1>5</Xingcode1>
      </item>
    </items>
  </xingming>
  <nianji>
    <item>
      <name>一年级</name>
      <name>一班</name>
    </item>
  </nianji>
</b>


代码如下:

 

 

  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             string xml = @"<a>  
  8.     <xingming>  
  9.         <item>  
  10.             <name>张三</name>  
  11.             <code>1</code>  
  12.         </item>  
  13.         <item>  
  14.             <name>李四</name>  
  15.             <code>2</code>  
  16.         </item>  
  17.         <item>  
  18.             <name>王五</name>  
  19.             <code>3</code>  
  20.         </item>  
  21.         <item>  
  22.             <name>赵四</name>  
  23.             <code>4</code>  
  24.         </item>  
  25.         <item>  
  26.             <name>王二麻子</name>  
  27.             <code>5</code>  
  28.         </item>  
  29.     </xingming>  
  30.     <nianji>  
  31.         <item>  
  32.             <name>一年级</name>  
  33.             <banji>一班</banji>  
  34.         </item>  
  35.     </nianji>  
  36. </a>";  
  37.   
  38.             XmlDocument newXmlDoc = new XmlDocument();  
  39.             XmlElement b = newXmlDoc.CreateElement("b");  
  40.             newXmlDoc.AppendChild(b);  
  41.   
  42.             XmlElement xingming = newXmlDoc.CreateElement("xingming");  
  43.             b.AppendChild(xingming);  
  44.   
  45.             XmlElement items = newXmlDoc.CreateElement("items");  
  46.             xingming.AppendChild(items);  
  47.   
  48.             XmlDocument xmlDoc = new XmlDocument();  
  49.             xmlDoc.LoadXml(xml);  
  50.   
  51.             XmlElement item = null;  
  52.   
  53.             //直接定位到/a/xingming节点下的子节点  
  54.             var nodes = xmlDoc.SelectSingleNode("/a/xingming").ChildNodes;  
  55.   
  56.             for (int i = 0; i < nodes.Count; i++)  
  57.             {  
  58.                 XmlNode node = nodes.Item(i);  
  59.   
  60.                 //节点项为偶数时,创建item节点  
  61.                 if (i % 2 == 0)  
  62.                 {  
  63.                     item = newXmlDoc.CreateElement("item");  
  64.                     items.AppendChild(item);  
  65.                 }  
  66.   
  67.                 //item节点追加Xingname节点  
  68.                 XmlElement Xingname = newXmlDoc.CreateElement("Xingname" + (i % 2 + 1));  
  69.                 Xingname.InnerText = node.SelectSingleNode("name").InnerText;  
  70.                 item.AppendChild(Xingname);  
  71.   
  72.                 //item节点追加Xingcode节点  
  73.                 XmlElement Xingcode = newXmlDoc.CreateElement("Xingcode" + (i % 2 + 1));  
  74.                 Xingcode.InnerText = node.SelectSingleNode("code").InnerText;  
  75.                 item.AppendChild(Xingcode);  
  76.   
  77.             }  
  78.   
  79.             XmlElement nianji = newXmlDoc.CreateElement("nianji");  
  80.             b.AppendChild(nianji);  
  81.   
  82.             XmlElement item1 = newXmlDoc.CreateElement("item");  
  83.             nianji.AppendChild(item1);  
  84.   
  85.             XmlElement name = newXmlDoc.CreateElement("name");  
  86.             name.InnerText = xmlDoc.SelectSingleNode("/a/nianji/item/name").InnerText;  
  87.             item1.AppendChild(name);  
  88.   
  89.             XmlElement banji = newXmlDoc.CreateElement("name");  
  90.             banji.InnerText = xmlDoc.SelectSingleNode("/a/nianji/item/banji").InnerText;  
  91.             item1.AppendChild(banji);  
  92.   
  93.             newXmlDoc.Save("b.xml");  
  94.         }  
  95.     }  
  96. }  
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = @"<a>
	<xingming>
		<item>
			<name>张三</name>
			<code>1</code>
		</item>
		<item>
			<name>李四</name>
			<code>2</code>
		</item>
		<item>
			<name>王五</name>
			<code>3</code>
		</item>
		<item>
			<name>赵四</name>
			<code>4</code>
		</item>
		<item>
			<name>王二麻子</name>
			<code>5</code>
		</item>
	</xingming>
	<nianji>
		<item>
			<name>一年级</name>
			<banji>一班</banji>
		</item>
	</nianji>
</a>";

            XmlDocument newXmlDoc = new XmlDocument();
            XmlElement b = newXmlDoc.CreateElement("b");
            newXmlDoc.AppendChild(b);

            XmlElement xingming = newXmlDoc.CreateElement("xingming");
            b.AppendChild(xingming);

            XmlElement items = newXmlDoc.CreateElement("items");
            xingming.AppendChild(items);

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            XmlElement item = null;

            //直接定位到/a/xingming节点下的子节点
            var nodes = xmlDoc.SelectSingleNode("/a/xingming").ChildNodes;

            for (int i = 0; i < nodes.Count; i++)
            {
                XmlNode node = nodes.Item(i);

                //节点项为偶数时,创建item节点
                if (i % 2 == 0)
                {
                    item = newXmlDoc.CreateElement("item");
                    items.AppendChild(item);
                }

                //item节点追加Xingname节点
                XmlElement Xingname = newXmlDoc.CreateElement("Xingname" + (i % 2 + 1));
                Xingname.InnerText = node.SelectSingleNode("name").InnerText;
                item.AppendChild(Xingname);

                //item节点追加Xingcode节点
                XmlElement Xingcode = newXmlDoc.CreateElement("Xingcode" + (i % 2 + 1));
                Xingcode.InnerText = node.SelectSingleNode("code").InnerText;
                item.AppendChild(Xingcode);

            }

            XmlElement nianji = newXmlDoc.CreateElement("nianji");
            b.AppendChild(nianji);

            XmlElement item1 = newXmlDoc.CreateElement("item");
            nianji.AppendChild(item1);

            XmlElement name = newXmlDoc.CreateElement("name");
            name.InnerText = xmlDoc.SelectSingleNode("/a/nianji/item/name").InnerText;
            item1.AppendChild(name);

            XmlElement banji = newXmlDoc.CreateElement("name");
            banji.InnerText = xmlDoc.SelectSingleNode("/a/nianji/item/banji").InnerText;
            item1.AppendChild(banji);

            newXmlDoc.Save("b.xml");
        }
    }
}



 

C#操作xml文件

标签:pac   article   oar   detail   进一步   namespace   app   string   好的   

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!