zoukankan      html  css  js  c++  java
  • 生成自定义的xml数据

    实例代码如下

    XmlDocument xmlDoc = new XmlDocument();            

    xmlDoc = new XmlDocument();            

    XmlDeclaration dec;            

    XmlElement elem;            

    XmlAttribute att;            

    dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);            

    xmlDoc.AppendChild(dec);

    elem = xmlDoc.CreateElement("test_data");            

    att = xmlDoc.CreateAttribute("version");            

    att.Value = "3.5";            

    elem.Attributes.Append(att);            

    xmlDoc.AppendChild(elem);

    //orders element            

    elem = xmlDoc.CreateElement("order");            

    att = xmlDoc.CreateAttribute("id");            

    att.Value = id;            

    elem.Attributes.Append(att);

    att = xmlDoc.CreateAttribute("code");            

    att.Value = code;            

    elem.Attributes.Append(att);            

    xmlDoc.DocumentElement.AppendChild(elem);

    att = xmlDoc.CreateAttribute("pass_code");            

    att.Value = pass_code;            

    elem.Attributes.Append(att);            

    xmlDoc.DocumentElement.AppendChild(elem);

    //products_all            

    elem = xmlDoc.CreateElement("products_all");            

    XmlNode node1 = xmlDoc.SelectSingleNode("descendant::test_data/order");            

    node1.AppendChild(elem);

    foreach (string product in products)            

    {                

    string[] productInfo = product.Split('|');                

    //product                

    elem = xmlDoc.CreateElement("product");                

    XmlNode node2 = xmlDoc.SelectSingleNode("descendant::fanli_data/order/products_all");                

    node2.AppendChild(elem);

    XmlNode node3 = node2.LastChild;                

    elem = xmlDoc.CreateElement("product_id");                

    elem.InnerText = productInfo[0];                

    node3.AppendChild(elem);

    elem = xmlDoc.CreateElement("product_url");                

    elem.InnerText = productInfo[1];                

    node3.AppendChild(elem);

    elem = xmlDoc.CreateElement("product_qty");                

    elem.InnerText = productInfo[2];                

    node3.AppendChild(elem);

    elem = xmlDoc.CreateElement("product_price");                

    elem.InnerText = productInfo[3];                

    node3.AppendChild(elem);

    }

  • 相关阅读:
    Windows10右键添加“在此处打开命令窗口"
    赋值简单理解
    应用 EditPlus 配置 Java 编译环境
    进程和线程的区别
    Java栈与堆
    进程与线程的简单解释
    java的多态性(二)
    内部类详解(很详细)
    java的super和this关键字用法总结
    Java类成员(成员变量和方法)的覆盖与隐藏归纳
  • 原文地址:https://www.cnblogs.com/liang-ling/p/3359344.html
Copyright © 2011-2022 走看看