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>

  • 相关阅读:
    《Python自动化运维:技术与最佳实践》
    舍本求末的运维自动化技术热潮
    Policy Gradients
    Machine Learning Notes Ⅵ
    Machine Learning Notes Ⅴ
    Machine Learning Notes Ⅳ
    Machine Learning Notes Ⅲ
    Machine Learning Notes Ⅱ
    Machine Learning Notes Ⅰ
    在Linux系统中如何把文件拷贝到U盘
  • 原文地址:https://www.cnblogs.com/fulai/p/3338288.html
Copyright © 2011-2022 走看看