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>

  • 相关阅读:
    国际标准:ISO 8601
    Python中文问题SyntaxError
    主人与狗
    三星Galaxy note I9220 N7000 刷机ROOT工具及ROM下载
    电子书下载:Pro ASP.NET 4 in C# 2010
    Rad Studio IDE 代码编辑器增强工具 RADSplit
    电子书下载:Essential Windows Phone 7.5
    3D成像法:抖动
    减小delphi生成EXE文件体积的方法
    unisynedit 在Delphi 2010下的编译问题
  • 原文地址:https://www.cnblogs.com/fulai/p/3338288.html
Copyright © 2011-2022 走看看