zoukankan      html  css  js  c++  java
  • XmlNode与XmlElement区别 转载的

    //XmlNode生成XML
    XmlDocument xmlnDoc = new XmlDocument();
    XmlNode xmlnRoot = xmlnDoc.CreateNode(XmlNodeType.Element, "nRoot", String.Empty);
    XmlNode itemNode = null;
    for (int i = 0; i < 3; i++)
    {
         itemNode = xmlnDoc.CreateNode(XmlNodeType.Element, "Customer", String.Empty);
         XmlNode codeNode = xmlnDoc.CreateNode(XmlNodeType.Element, "Code", String.Empty);
         codeNode.InnerText = "Code"+i.ToString();
         XmlNode nameNode = xmlnDoc.CreateNode(XmlNodeType.Element, "Name", String.Empty);
         nameNode.InnerText = "Name"+i.ToString();
         itemNode.AppendChild(codeNode);
         itemNode.AppendChild(nameNode);

         xmlnRoot.AppendChild(itemNode);
     }
    xmlnDoc.AppendChild(xmlnRoot);

    生成的结果为:

    <nRoot>
        <Customer>
           <Code>Code0</Code>
           <Name>Name0</Name>
        </Customer>
        <Customer>
           <Code>Code1</Code>
           <Name>Name1</Name>
        </Customer>
        <Customer>
           <Code>Code2</Code>
           <Name>Name2</Name>
        </Customer>
    </nRoot>

    //XmlElement生成XML
    XmlDocument xmleDoc = new XmlDocument();
    XmlElement xmleRoot = xmleDoc.CreateElement("eRoot");
    xmleDoc.AppendChild(xmleRoot);
    for (int i = 0; i < 3; i++)
    {
        XmlElement xmlEle = xmleDoc.CreateElement("Customer");
        xmlEle.SetAttribute("Code","Code"+i.ToString());
        xmlEle.SetAttribute("Name", "Name" + i.ToString());

        xmlEle.InnerText = "Code" + i.ToString();
        xmleRoot.AppendChild(xmlEle);
     }

    生成的结果为:

    <eRoot>
       <Customer Code="Code0" Name="Name0">Code0</Customer>
       <Customer Code="Code1" Name="Name1">Code1</Customer>
       <Customer Code="Code2" Name="Name2">Code2</Customer>
    </eRoot>

  • 相关阅读:
    listView控件演示程序
    猪悟能淘宝商品下载专家v3版开发手记
    c# WebClient类
    分隔控件splitter演示程序
    C#中SESSIONID的获取
    工具栏toolBar演示程序
    Cookie总结
    C#获取网页源码并且自动判断网页字符集编码
    如何在Google Code上建立私有代码版本库
    iOS应用程序生命周期(前后台切换,应用的各种状态)详解
  • 原文地址:https://www.cnblogs.com/fulai/p/3338288.html
Copyright © 2011-2022 走看看