zoukankan      html  css  js  c++  java
  • C#给XmlNode节点添加Name属性

     准备生成的XML文件格式如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <Update>
      <Soft Name="BlogWriter">
        <Verson>1.0.1.2</Verson>
        <DownLoad>http://www.csdn.net/BlogWrite.rar</DownLoad>
      </Soft>
    </Update>

    详细代码为:

                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                //创建一个根节点(一级)
                XmlElement root = doc.CreateElement("Update");
                doc.AppendChild(root);
                //创建节点(二级)
                XmlNode node = doc.CreateElement("Soft");
                node.Attributes.Append(CreateAttribute(node, "Name", "BlogWriter"));
                //创建节点(三级)
                XmlElement element1 = doc.CreateElement("Verson");
                element1.InnerText = "1.0.1.2";
                node.AppendChild(element1);

                XmlElement element2 = doc.CreateElement("DownLoad");
                element2.InnerText = "http://www.csdn.net/BlogWrite.rar";
                node.AppendChild(element2);

                root.AppendChild(node);
                doc.Save(@"C:\web\bb.xml");
                Console.Write(doc.OuterXml);

    添加节点属性方法

           public XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
            {
                try
                {
                    XmlDocument doc = node.OwnerDocument;
                    XmlAttribute attr = null;
                    attr = doc.CreateAttribute(attributeName);
                    attr.Value = value;
                    node.Attributes.SetNamedItem(attr);
                    return attr;
                }
                catch (Exception err)
                {
                    string desc = err.Message;
                    return null;
                }
            } 

    需要添加的命名空间为using System.Xml;

  • 相关阅读:
    Windows Media Player Audio normalize wmp音量规格化
    IE6、IE7、IE8不支持XHTML,不过没关系
    w3.org出现HTML语法错误
    主板bios修改、刷新
    HTML原则:内容放在HTML中,不要依赖CSS
    textarea应使用cols和rows来控制width和height,而不应只使用css
    sata AHCI驱动下载(AMD Intel Nvidia)
    咬文嚼字:中国文字、中国话
    详解W3C标准:html 4.01中的lang属性——实际上它是一个刮胡刀
    热烈欢呼:cnblogs.com博客园首页通过W3C验证
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306771.html
Copyright © 2011-2022 走看看