<?xml version="1.0" encoding="UTF-8"?>  
    <books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:noNamespaceSchemaLocation="t3.xsd">  
        <book id="001" type="IT">  
            <bookName>mastering XML</bookName>  
            <author>Jack LEE</author>  
            <ISBN>0-125-11111-6</ISBN>  
            <price>36.00</price>  
            <quantity>6</quantity>  
        </book>  
        <book id="002" type="education">  
            <bookName>the method of study</bookName>  
            <author>Thomas</author>  
            <ISBN>0-125-22222-7</ISBN>  
            <price>45.00</price>  
            <quantity>5</quantity>  
        </book>  
        <book id="003" type="novel">  
            <bookName>The hero</bookName>  
            <author>TuDou</author>  
            <ISBN>0-125-88888-6</ISBN>  
            <price>88.00</price>  
            <quantity>2</quantity>  
        </book>  
    </books>  id,type需要转成字段,这段格式就是调用别人Service返回的结果,他们不可能去改结果的。 

解决方案 »

  1.   

    你得知道怎么解析XML
    XMLDocument 
    XElement 
    2种方法都可以
      

  2.   

    参见:http://www.cnblogs.com/lilin/archive/2010/04/18/1714927.html可以将xml转成DataTable对象
      

  3.   

        public class books
        {
            [XmlElement("book")]
            public book[] book { get; set; }
        }
        public class book
        {
            [XmlAttribute("id")]
            public string id { get; set; }
            [XmlAttribute("type")]
            public string type { get; set; }
            public string bookName { get; set; }
            public string author { get; set; }
            public string ISBN { get; set; }
            public double price { get; set; }
            public int quantity { get; set; }
        }
                string str = @"<?xml version=""1.0"" encoding=""UTF-8""?>   
      <books xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""   
      xsi:noNamespaceSchemaLocation=""t3.xsd"">   
      <book id=""001"" type=""IT"">   
      <bookName>mastering XML</bookName>   
      <author>Jack LEE</author>   
      <ISBN>0-125-11111-6</ISBN>   
      <price>36.00</price>   
      <quantity>6</quantity>   
      </book>   
      <book id=""002"" type=""education"">   
      <bookName>the method of study</bookName>   
      <author>Thomas</author>   
      <ISBN>0-125-22222-7</ISBN>   
      <price>45.00</price>   
      <quantity>5</quantity>   
      </book>   
      <book id=""003"" type=""novel"">   
      <bookName>The hero</bookName>   
      <author>TuDou</author>   
      <ISBN>0-125-88888-6</ISBN>   
      <price>88.00</price>   
      <quantity>2</quantity>   
      </book>   
      </books>";
                books books = new books();
                XmlSerializer xml = new XmlSerializer(books.GetType());
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str));
                books = xml.Deserialize(ms) as books;
                ms.Dispose();
                foreach (book b in books.book)
                    Console.WriteLine("{0}={1}={2}={3}={4}={5}={6}", b.id, b.type, b.bookName, b.author, b.ISBN, b.price.ToString(), b.quantity);
    /*
    001=IT=mastering XML=Jack LEE=0-125-11111-6=36=6
    002=education=the method of study=Thomas=0-125-22222-7=45=5
    003=novel=The hero=TuDou=0-125-88888-6=88=2*/
      

  4.   


    貌似例子的xml结构不够复杂
      

  5.   

    http://blog.csdn.net/envykok/article/details/5659753http://hi.baidu.com/%B6%FA%D7%D3/blog/item/d5b48dfb1a30d452252df280.html
      

  6.   


    马上尝试一下,如果book里面还内嵌一个比较出版社的详细信息该如何修改呢
      

  7.   

    <FinalResults curapiversion="1.6.1" Testappversion="1.5" responsetime="0.0111111">
    <status>success</status>
    <Inventory id="246354">
    <response id="12345">
           Deleted
        </response>
        <response id="123456">
           Deleted
        </response>
    </Inventory>
    </FinalResults>红色部分是Service返回的统一格式,连 XML头都没有了,楼上兄弟提供的几招似乎都用不上了。
      

  8.   

    那就直接用xmldocument操作            XmlDocument xml = new XmlDocument();
                xml.LoadXml(@"<FinalResults curapiversion=""1.6.1"" Testappversion=""1.5"" responsetime=""0.0111111"">
                                <status>success</status>
                                <Inventory id=""246354"">
                                    <response id=""12345"">
                                        Deleted
                                    </response>
                                    <response id=""123456"">
                                        Deleted
                                    </response>
                                </Inventory>
                            </FinalResults>");
                XmlNode status = xml.SelectSingleNode("/FinalResults/status");
                Console.WriteLine(status.InnerText);
                XmlNodeList responseID = xml.SelectNodes("/FinalResults/Inventory/response");
                foreach (XmlNode n in responseID)
                    Console.WriteLine(n.Attributes["id"].Value);
    /*
    其他节点类似这样取。
    success
    12345
    123456*/
      

  9.   

    1、建立对应实体类:   [XmlRoot]
        public class books
        {
            [XmlArray]
            [XmlArrayItem("book", typeof(book))]
            public book[] book { get; set; }
        }
        
        public class book
        {
            [XmlAttribute("id")]
            public string id { get; set; }
            [XmlAttribute("type")]
            public string type { get; set; }
            [XmlElement]
            public string bookName { get; set; }
            [XmlElement]
            public string author { get; set; }
            [XmlElement]
            public string ISBN { get; set; }
            [XmlElement]
            public double price { get; set; }
            [XmlElement]
            public int quantity { get; set; }
        }
    2、加入重载属性定义:        private void button6_Click(object sender, EventArgs e)
            {
                try
                {
                    XmlAttributes attrs = new XmlAttributes();
                    attrs.XmlElements.Add(new XmlElementAttribute("book", typeof(book)));
                    XmlAttributeOverrides attrOver = new XmlAttributeOverrides();//这玩意是控制Xml重新输出和加载
                    attrOver.Add(typeof(Inventroty), "book", attrs);
                    FileStream fs = new FileStream(@"c:\test.xml", FileMode.Open);
                    
                    XmlSerializer sr= new XmlSerializer(typeof(books),attrOver);
                    books bs= (Inventroty)sr.Deserialize(fs);
                    fs.Close();            }
                catch (Exception ex)
                {            }
            }
      

  10.   

    http://blog.sina.com.cn/s/blog_70e9c0d40100lpq5.html