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>

  • 相关阅读:
    数值的格式化
    最近开发的一个多功能选择器
    OCI22053: 溢出错误的原因和解决方法
    js的四舍五入问题
    Vista 下先安装 VS 再装 IIS 7 运行出现 500.19 Error 的解决方法
    最近开发的一个多功能选择器2
    Oracle REF Cursor 用法(转帖)
    Oracle 查询历史数据(转帖)
    HDU 1203 I NEED A OFFER! 动态规划(01背包)
    HDU 1160 FatMouse's Speed 动态规划
  • 原文地址:https://www.cnblogs.com/fulai/p/3338288.html
Copyright © 2011-2022 走看看