首页 新闻 会员 周边 捐助

真心求助,XML解析问题???

0
悬赏园豆:30 [已关闭问题] 关闭于 2012-07-04 15:07

<result>
<resultcode>0</resultcode>
<resultmessage>全部成功</resultmessage>
<swssupplierid>ceshi</swssupplierid>
<resultdetail>
<allordernum>213</allordernum>
<allpagenum>11</allpagenum>
<nowpage>1</nowpage>
<orderlist>
<order>
<orderid>123</orderid>
<orderdistributetime>456</orderdistributetime>
<orderdetail>
<barcode>001</barcode>
<sku>002</sku>
</orderdetail>
</order>
<order>
<orderid>002</orderid>
<orderdistributetime>789</orderdistributetime>
<orderdetail>
<barcode>006</barcode>
<sku>007</sku>
</orderdetail>
<orderdetail>
<barcode>008</barcode>
<sku>009</sku>
</orderdetail>
</order>
</orderlist>
</resultdetail>
</result>

如上的XML,怎样遍历这个XML找到 <order>里面的 <orderdetail>在<orderdetail>的上面加一层节点<orderdetails>!!!

例如加完后

<order>
<orderid>123</orderid>
<orderdistributetime>456</orderdistributetime>

<orderdetails>
<orderdetail>
<barcode>001</barcode>
<sku>002</sku>
</orderdetail>

</orderdetails>
</order>

<order>
<orderid>123</orderid>
<orderdistributetime>456</orderdistributetime>

<orderdetails>
<orderdetail>
<barcode>001</barcode>
<sku>002</sku>
</orderdetail>

<orderdetail>
<barcode>001</barcode>
<sku>002</sku>
</orderdetail>

</orderdetails>
</order>

XML
lingxiayidu的主页 lingxiayidu | 初学一级 | 园豆:31
提问于:2012-07-01 14:01
< >
分享
所有回答(2)
0
下面是刚才动手给你写的demo!不知道是不是你想要的。
在此先凑合了哈!

1
using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml.Serialization; 6 using System.IO; 7 8 namespace ConsoleApplication1 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 Root r = new Root(); 15 16 r.Orders = new List<Order>(); 17 Order ms = new Order(); 18 ms.Orderid = "ID0001"; 19 ms.Orderdistributetime = "356"; 20 ms.Orderdetails = new Orderdetails(); 21 ms.Orderdetails.Orderdetail = new List<Orderdetail>(); 22 23 Orderdetail it = new Orderdetail(); 24 it.Barcode = "001"; 25 it.Sku = "002"; 26 ms.Orderdetails.Orderdetail.Add(it); 27 ms.Orderdetails.Orderdetail.Add(it); 28 29 Orderdetail it2 = new Orderdetail(); 30 it2.Barcode = "0011"; 31 it2.Sku = "0022"; 32 33 r.Orders.Add(ms); 34 ms.Orderdetails.Orderdetail.Add(it2); 35 r.Orders.Add(ms); 36 37 Console.WriteLine("序列化成功……"); 38 string sttr = XmlSerialize.SerializeXML<Root>(r); 39 Console.WriteLine(sttr); 40 41 Root d= XmlSerialize.DeserializeXML<Root>(sttr); 42 43 Console.ReadLine(); 44 } 45 } 46 [Serializable] 47 public class Root 48 { 49 public List<Order> Orders; 50 } 51 52 [Serializable] 53 public class Order 54 { 55 public string Orderid; 56 57 public string Orderdistributetime; 58 59 public Orderdetails Orderdetails; 60 61 } 62 63 [Serializable] 64 public class Orderdetails 65 { 66 67 [XmlElement] 68 public List<Orderdetail> Orderdetail; 69 } 70 71 public class Orderdetail 72 { 73 public string Barcode; 74 public string Sku; 75 76 77 } 78 79 public class XmlSerialize 80 { 81 /// <summary> 82 /// 反序列化XML为类实例 83 /// </summary> 84 /// <typeparam name="T"></typeparam> 85 /// <param name="xmlObj"></param> 86 /// <returns></returns> 87 public static T DeserializeXML<T>(string xmlObj) 88 { 89 XmlSerializer serializer = new XmlSerializer(typeof(T)); 90 using (StringReader reader = new StringReader(xmlObj)) 91 { 92 return (T)serializer.Deserialize(reader); 93 } 94 } 95 96 /// <summary> 97 /// 序列化类实例为XML 98 /// </summary> 99 /// <typeparam name="T"></typeparam> 100 /// <param name="obj"></param> 101 /// <returns></returns> 102 public static string SerializeXML<T>(T obj) 103 { 104 using (StringWriter writer = new StringWriter()) 105 { 106 XmlSerializerNamespaces nspaces = new XmlSerializerNamespaces(); 107 nspaces.Add("",""); 108 new XmlSerializer(obj.GetType()).Serialize((TextWriter)writer, obj, nspaces); 109 return writer.ToString(); 110 } 111 } 112 } 113 114 } 115 ======输出结果============== 116 117 <?xml version="1.0" encoding="utf-16"?> 118 <Root> 119 <Orders> 120 <Order> 121 <Orderid>ID0001</Orderid> 122 <Orderdistributetime>356</Orderdistributetime> 123 <Orderdetails> 124 <Orderdetail> 125 <Barcode>001</Barcode> 126 <Sku>002</Sku> 127 </Orderdetail> 128 <Orderdetail> 129 <Barcode>001</Barcode> 130 <Sku>002</Sku> 131 </Orderdetail> 132 <Orderdetail> 133 <Barcode>0011</Barcode> 134 <Sku>0022</Sku> 135 </Orderdetail> 136 </Orderdetails> 137 </Order> 138 <Order> 139 <Orderid>ID0001</Orderid> 140 <Orderdistributetime>356</Orderdistributetime> 141 <Orderdetails> 142 <Orderdetail> 143 <Barcode>001</Barcode> 144 <Sku>002</Sku> 145 </Orderdetail> 146 <Orderdetail> 147 <Barcode>001</Barcode> 148 <Sku>002</Sku> 149 </Orderdetail> 150 <Orderdetail> 151 <Barcode>0011</Barcode> 152 <Sku>0022</Sku> 153 </Orderdetail> 154 </Orderdetails> 155 </Order> 156 </Orders> 157 </Root>
po~饭盒 | 园豆:231 (菜鸟二级) | 2012-07-01 17:27

你好,我的想法是动态去添加<Orderdetails></Orderdetails>,因为Order的数量是不固定,Orderdetail的数量也是不固定的。所以......

支持(0) 反对(0) lingxiayidu | 园豆:31 (初学一级) | 2012-07-02 14:16
0

第一步:使用xsd工具将xml文件生成可序列化的对象(例如下面的xml文件)

<?xml version="1.0" encoding="utf-8" ?>
<configuration  xmlns="Moops.Info" >
  <TaskList>
     <TaskInfo>
      <TaskID>1</TaskID>
      <Key>datatime</Key>
      <DataType>datatype</DataType>
      <TaskTemplate>VNET.xls</TaskTemplate>
      <TaskName>集团V网新增占比通报</TaskName>
      <DestnationDir>Vnet_</DestnationDir>
      <SheetArea IsSeparate="False" name="datamonth">
        <DataArea>
          <data datatype="Vnet_1000001" start_column="O1" end_column="P1" column_name="加法人集团" data_column_name="DATAEXP,DATAVALUE"></data>
          <data datatype="Vnet_1000002" start_column="S1" end_column="T1" column_name="完全新增集团用户"  data_column_name="DATAEXP,DATAVALUE"></data>
          <data datatype="Vnet_1000003" start_column="E1" end_column="F1" column_name="加法人集团"  data_column_name="DATAEXP,DATAVALUE"></data>
          <data datatype="Vnet_1000004" start_column="I1" end_column="J1" column_name="完全新增集团用户"  data_column_name="DATAEXP,DATAVALUE"></data>
          <data datatype="Vnet_2000001" start_column="M1" end_column="N1" column_name="加乡情网"  data_column_name="DATAEXP,DATAVALUE"></data>
                </DataArea>
      </SheetArea>
    </TaskInfo>
  </TaskList>
</configuration>


通过xsd工具生成的对象文件如下:

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.225
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// 此源代码由 xsd 自动生成, Version=4.0.30319.1。
// 
namespace Moops.Info.Configuration
{


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "Moops.Info")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "Moops.Info", IsNullable = false)]
    public partial class configuration
    {

        private configurationTaskInfo[] taskListField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("TaskInfo", IsNullable = false)]
        public configurationTaskInfo[] TaskList
        {
            get
            {
                return this.taskListField;
            }
            set
            {
                this.taskListField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "Moops.Info")]
    public partial class configurationTaskInfo
    {

        private string taskIDField;

        private string keyField;

        private string dataTypeField;

        private string taskTemplateField;

        private string taskNameField;

        private string destnationDirField;

        private configurationTaskInfoSheetArea[] sheetAreaField;

        /// <remarks/>
        public string TaskID
        {
            get
            {
                return this.taskIDField;
            }
            set
            {
                this.taskIDField = value;
            }
        }

        /// <remarks/>
        public string Key
        {
            get
            {
                return this.keyField;
            }
            set
            {
                this.keyField = value;
            }
        }

        /// <remarks/>
        public string DataType
        {
            get
            {
                return this.dataTypeField;
            }
            set
            {
                this.dataTypeField = value;
            }
        }

        /// <remarks/>
        public string TaskTemplate
        {
            get
            {
                return this.taskTemplateField;
            }
            set
            {
                this.taskTemplateField = value;
            }
        }

        /// <remarks/>
        public string TaskName
        {
            get
            {
                return this.taskNameField;
            }
            set
            {
                this.taskNameField = value;
            }
        }

        /// <remarks/>
        public string DestnationDir
        {
            get
            {
                return this.destnationDirField;
            }
            set
            {
                this.destnationDirField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("SheetArea")]
        public configurationTaskInfoSheetArea[] SheetArea
        {
            get
            {
                return this.sheetAreaField;
            }
            set
            {
                this.sheetAreaField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "Moops.Info")]
    public partial class configurationTaskInfoSheetArea
    {

        private configurationTaskInfoSheetAreaData[] dataAreaField;

        private string isSeparateField;

        private string nameField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("data", IsNullable = false)]
        public configurationTaskInfoSheetAreaData[] DataArea
        {
            get
            {
                return this.dataAreaField;
            }
            set
            {
                this.dataAreaField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string IsSeparate
        {
            get
            {
                return this.isSeparateField;
            }
            set
            {
                this.isSeparateField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "Moops.Info")]
    public partial class configurationTaskInfoSheetAreaData
    {

        private string datatypeField;

        private string start_columnField;

        private string end_columnField;

        private string column_nameField;

        private string data_column_nameField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string datatype
        {
            get
            {
                return this.datatypeField;
            }
            set
            {
                this.datatypeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string start_column
        {
            get
            {
                return this.start_columnField;
            }
            set
            {
                this.start_columnField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string end_column
        {
            get
            {
                return this.end_columnField;
            }
            set
            {
                this.end_columnField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string column_name
        {
            get
            {
                return this.column_nameField;
            }
            set
            {
                this.column_nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string data_column_name
        {
            get
            {
                return this.data_column_nameField;
            }
            set
            {
                this.data_column_nameField = value;
            }
        }
    }
}

这个CS对象文件是通过xsd工具生成的,只需要在需要调用的地方使用xml的序列化语句。

例如:

  private Moops.Info.Configuration.configuration GetConfig()
        {
            Moops.Info.Configuration.configuration myConfig = null;
            string XmlFilePath = Application.StartupPath + "\\MyConfig.config";
            XmlSerializer xs = new XmlSerializer(typeof(Moops.Info.Configuration.configuration));
            using (FileStream fs = new FileStream(XmlFilePath, FileMode.Open))
            {
                myConfig = (Moops.Info.Configuration.configuration)xs.Deserialize(fs);
            }
            return myConfig;
        }

 

上面是反序列化,序列化可以这样

 private void SerializeObject(string Xmlname)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Moops.Info.Configuration.configuration));
            Moops.Info.Configuration.configuration mycon = new Moops.Info.Configuration.configuration();
            TextWriter writer = new StreamWriter(Xmlname);
            ser.Serialize(writer, mycon);//要序列化的对象
            writer.Close();
        }

massinger | 园豆:706 (小虾三级) | 2012-07-01 21:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册