如何反序列化XML文档

时间:2008-12-12 21:49:59

标签: c# asp.net xml serialization xml-deserialization

如何反序列化此XML文档:

<?xml version="1.0" encoding="utf-8"?>
<Cars>
  <Car>
    <StockNumber>1020</StockNumber>
    <Make>Nissan</Make>
    <Model>Sentra</Model>
  </Car>
  <Car>
    <StockNumber>1010</StockNumber>
    <Make>Toyota</Make>
    <Model>Corolla</Model>
  </Car>
  <Car>
    <StockNumber>1111</StockNumber>
    <Make>Honda</Make>
    <Model>Accord</Model>
  </Car>
</Cars>

我有这个:

[Serializable()]
public class Car
{
    [System.Xml.Serialization.XmlElementAttribute("StockNumber")]
    public string StockNumber{ get; set; }

    [System.Xml.Serialization.XmlElementAttribute("Make")]
    public string Make{ get; set; }

    [System.Xml.Serialization.XmlElementAttribute("Model")]
    public string Model{ get; set; }
}

[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
    [XmlArrayItem(typeof(Car))]
    public Car[] Car { get; set; }

}

public class CarSerializer
{
    public Cars Deserialize()
    {
        Cars[] cars = null;
        string path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data/") + "cars.xml";

        XmlSerializer serializer = new XmlSerializer(typeof(Cars[]));

        StreamReader reader = new StreamReader(path);
        reader.ReadToEnd();
        cars = (Cars[])serializer.Deserialize(reader);
        reader.Close();

        return cars;
    }
}

似乎不起作用: - (

17 个答案:

答案 0 :(得分:418)

如何将xml保存到文件中,并使用xsd生成C#类?

  1. 将文件写入磁盘(我将其命名为foo.xml)
  2. 生成xsd:xsd foo.xml
  3. 生成C#:xsd foo.xsd /classes
  4. Et voila - 以及应该能够通过XmlSerializer读取数据的C#代码文件:

        XmlSerializer ser = new XmlSerializer(typeof(Cars));
        Cars cars;
        using (XmlReader reader = XmlReader.Create(path))
        {
            cars = (Cars) ser.Deserialize(reader);
        }
    

    (包括项目中生成的foo.cs)

答案 1 :(得分:331)

这是一个有效的版本。我将XmlElementAttribute标签更改为XmlElement,因为在xml中,StockNumber,Make和Model值是元素,而不是属性。我也删除了reader.ReadToEnd(); (function读取整个流并返回一个字符串,因此Deserialze()函数不再使用读取器......位置位于流的末尾)。我也对命名采取了一些自由:)。

以下是课程:

[Serializable()]
public class Car
{
    [System.Xml.Serialization.XmlElement("StockNumber")]
    public string StockNumber { get; set; }

    [System.Xml.Serialization.XmlElement("Make")]
    public string Make { get; set; }

    [System.Xml.Serialization.XmlElement("Model")]
    public string Model { get; set; }
}


[Serializable()]
[System.Xml.Serialization.XmlRoot("CarCollection")]
public class CarCollection
{
    [XmlArray("Cars")]
    [XmlArrayItem("Car", typeof(Car))]
    public Car[] Car { get; set; }
}

反序列化功能:

CarCollection cars = null;
string path = "cars.xml";

XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));

StreamReader reader = new StreamReader(path);
cars = (CarCollection)serializer.Deserialize(reader);
reader.Close();

稍微调整一下的xml(我需要添加一个新元素来包装&lt; Cars&gt; ...... Net对于反序列化数组很挑剔):

<?xml version="1.0" encoding="utf-8"?>
<CarCollection>
<Cars>
  <Car>
    <StockNumber>1020</StockNumber>
    <Make>Nissan</Make>
    <Model>Sentra</Model>
  </Car>
  <Car>
    <StockNumber>1010</StockNumber>
    <Make>Toyota</Make>
    <Model>Corolla</Model>
  </Car>
  <Car>
    <StockNumber>1111</StockNumber>
    <Make>Honda</Make>
    <Model>Accord</Model>
  </Car>
</Cars>
</CarCollection>

答案 2 :(得分:215)

你有两种可能性。

方法1. XSD 工具

<小时/> 假设您的XML文件位于此位置C:\path\to\xml\file.xml

  1. 打开开发人员命令提示
    你可以在Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools找到它 或者如果您有Windows 8,只需在开始屏幕
  2. 中键入开发人员命令提示符即可
  3. 通过键入cd /D "C:\path\to\xml"
  4. 将位置更改为XML文件目录
  5. 通过键入xsd file.xml
  6. 从您的xml文件创建 XSD文件
  7. 输入xsd /c file.xsd
  8. 创建 C#类

    就是这样!您已在C:\path\to\xml\file.cs

    中的xml文件中生成了C#类

    方法2 - 粘贴特殊

    <小时/> 必需的Visual Studio 2012 +

    1. 将XML文件的内容复制到剪贴板
    2. 向您的解决方案添加新的空类文件( Shift + Alt + C
    3. 打开该文件,然后在菜单中点击Edit > Paste special > Paste XML As Classes
      enter image description here
    4. 就是这样!

      用法


      使用此助手类非常简单:

      using System;
      using System.IO;
      using System.Web.Script.Serialization; // Add reference: System.Web.Extensions
      using System.Xml;
      using System.Xml.Serialization;
      
      namespace Helpers
      {
          internal static class ParseHelpers
          {
              private static JavaScriptSerializer json;
              private static JavaScriptSerializer JSON { get { return json ?? (json = new JavaScriptSerializer()); } }
      
              public static Stream ToStream(this string @this)
              {
                  var stream = new MemoryStream();
                  var writer = new StreamWriter(stream);
                  writer.Write(@this);
                  writer.Flush();
                  stream.Position = 0;
                  return stream;
              }
      
      
              public static T ParseXML<T>(this string @this) where T : class
              {
                  var reader = XmlReader.Create(@this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
                  return new XmlSerializer(typeof(T)).Deserialize(reader) as T;
              }
      
              public static T ParseJSON<T>(this string @this) where T : class
              {
                  return JSON.Deserialize<T>(@this.Trim());
              }
          }
      }
      

      现在你所要做的就是:

          public class JSONRoot
          {
              public catalog catalog { get; set; }
          }
          // ...
      
          string xml = File.ReadAllText(@"D:\file.xml");
          var catalog1 = xml.ParseXML<catalog>();
      
          string json = File.ReadAllText(@"D:\file.json");
          var catalog2 = json.ParseJSON<JSONRoot>();
      

答案 3 :(得分:80)

以下代码段可以解决问题(您可以忽略大多数序列化属性):

public class Car
{
  public string StockNumber { get; set; }
  public string Make { get; set; }
  public string Model { get; set; }
}

[XmlRootAttribute("Cars")]
public class CarCollection
{
  [XmlElement("Car")]
  public Car[] Cars { get; set; }
}

...

using (TextReader reader = new StreamReader(path))
{
  XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
  return (CarCollection) serializer.Deserialize(reader);
}

答案 4 :(得分:22)

看看这是否有帮助:

[Serializable()]
[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
    [XmlArrayItem(typeof(Car))]
    public Car[] Car { get; set; }
}

[Serializable()]
public class Car
{
    [System.Xml.Serialization.XmlElement()]
    public string StockNumber{ get; set; }

    [System.Xml.Serialization.XmlElement()]
    public string Make{ get; set; }

    [System.Xml.Serialization.XmlElement()]
    public string Model{ get; set; }
}

如果没有使用visual studio附带的xsd.exe程序来创建基于该xml文件的模式文档,然后再次使用它来创建基于模式文档的类。

答案 5 :(得分:9)

我不认为.net对于反序列化数组是'挑剔'。第一个xml文档格式不正确。 没有根元素,虽然它看起来像。规范的xml文档具有根和至少1个元素(如果有的话)。在您的示例中:

<Root> <-- well, the root
  <Cars> <-- an element (not a root), it being an array
    <Car> <-- an element, it being an array item
    ...
    </Car>
  </Cars>
</Root>

答案 6 :(得分:7)

如果您的.xml文件已在磁盘中的某处生成并且使用了List<T>,请尝试此代码块:

//deserialization

XmlSerializer xmlser = new XmlSerializer(typeof(List<Item>));
StreamReader srdr = new StreamReader(@"C:\serialize.xml");
List<Item> p = (List<Item>)xmlser.Deserialize(srdr);
srdr.Close();`

注意:C:\serialize.xml是我的.xml文件的路径。您可以根据需要进行更改。

答案 7 :(得分:6)

除了事实之外,凯文的好处是好的,在现实世界中,你经常无法改变原始的XML以满足你的需求。

原始XML也是一个简单的解决方案:

[XmlRoot("Cars")]
public class XmlData
{
    [XmlElement("Car")]
    public List<Car> Cars{ get; set; }
}

public class Car
{
    public string StockNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
}

然后你可以简单地打电话:

var ser = new XmlSerializer(typeof(XmlData));
XmlData data = (XmlData)ser.Deserialize(XmlReader.Create(PathToCarsXml));

答案 8 :(得分:4)

初学者

我发现这里的答案非常有用,说我仍然努力(只是一点点)让这个工作。因此,万一它可以帮助某人,我会说明工作解决方案:

来自原始问题的XML。 xml位于文件Class1.xml中,此文件的path用于代码中以查找此xml文件。

我使用了@erymski的答案来实现这一点,因此创建了一个名为Car.cs的文件,并添加了以下内容:

using System.Xml.Serialization;  // Added

public class Car
{
    public string StockNumber { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
}

[XmlRootAttribute("Cars")]
public class CarCollection
{
    [XmlElement("Car")]
    public Car[] Cars { get; set; }
}

@erymski提供的另一段代码......

using (TextReader reader = new StreamReader(path))
{
  XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
  return (CarCollection) serializer.Deserialize(reader);
}

...进入你的主程序(Program.cs),static CarCollection XCar()就像这样:

using System;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApp2
{
    class Program
    {

        public static void Main()
        {
            var c = new CarCollection();

            c = XCar();

            foreach (var k in c.Cars)
            {
                Console.WriteLine(k.Make + " " + k.Model + " " + k.StockNumber);
            }
            c = null;
            Console.ReadLine();

        }
        static CarCollection XCar()
        {
            using (TextReader reader = new StreamReader(@"C:\Users\SlowLearner\source\repos\ConsoleApp2\ConsoleApp2\Class1.xml"))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
                return (CarCollection)serializer.Deserialize(reader);
            }
        }
    }
}

希望它有所帮助: - )

答案 9 :(得分:4)

试用这个Xml序列化的通用类&amp;反序列化。

public class SerializeConfig<T> where T : class
{
    public static void Serialize(string path, T type)
    {
        var serializer = new XmlSerializer(type.GetType());
        using (var writer = new FileStream(path, FileMode.Create))
        {
            serializer.Serialize(writer, type);
        }
    }

    public static T DeSerialize(string path)
    {
        T type;
        var serializer = new XmlSerializer(typeof(T));
        using (var reader = XmlReader.Create(path))
        {
            type = serializer.Deserialize(reader) as T;
        }
        return type;
    }
}

答案 10 :(得分:2)

一个班轮:

var object = (Cars)new XmlSerializer(typeof(Cars)).Deserialize(new StringReader(xmlString));

答案 11 :(得分:2)

我们的想法是为反序列化处理所有级别 请参阅解决我类似问题的示例解决方案

<?xml version="1.0" ?> 
 <TRANSACTION_RESPONSE>
    <TRANSACTION>
        <TRANSACTION_ID>25429</TRANSACTION_ID> 
        <MERCHANT_ACC_NO>02700701354375000964</MERCHANT_ACC_NO> 
        <TXN_STATUS>F</TXN_STATUS> 
        <TXN_SIGNATURE>a16af68d4c3e2280e44bd7c2c23f2af6cb1f0e5a28c266ea741608e72b1a5e4224da5b975909cc43c53b6c0f7f1bbf0820269caa3e350dd1812484edc499b279</TXN_SIGNATURE> 
        <TXN_SIGNATURE2>B1684258EA112C8B5BA51F73CDA9864D1BB98E04F5A78B67A3E539BEF96CCF4D16CFF6B9E04818B50E855E0783BB075309D112CA596BDC49F9738C4BF3AA1FB4</TXN_SIGNATURE2> 
        <TRAN_DATE>29-09-2015 07:36:59</TRAN_DATE> 
        <MERCHANT_TRANID>150929093703RUDZMX4</MERCHANT_TRANID> 
        <RESPONSE_CODE>9967</RESPONSE_CODE> 
        <RESPONSE_DESC>Bank rejected transaction!</RESPONSE_DESC> 
        <CUSTOMER_ID>RUDZMX</CUSTOMER_ID> 
        <AUTH_ID /> 
        <AUTH_DATE /> 
        <CAPTURE_DATE /> 
        <SALES_DATE /> 
        <VOID_REV_DATE /> 
        <REFUND_DATE /> 
        <REFUND_AMOUNT>0.00</REFUND_AMOUNT> 
    </TRANSACTION>
  </TRANSACTION_RESPONSE> 

上述XML在两个级别处理

  [XmlType("TRANSACTION_RESPONSE")]
public class TransactionResponse
{
    [XmlElement("TRANSACTION")]
    public BankQueryResponse Response { get; set; }

}

内在水平

public class BankQueryResponse
{
    [XmlElement("TRANSACTION_ID")]
    public string TransactionId { get; set; }

    [XmlElement("MERCHANT_ACC_NO")]
    public string MerchantAccNo { get; set; }

    [XmlElement("TXN_SIGNATURE")]
    public string TxnSignature { get; set; }

    [XmlElement("TRAN_DATE")]
    public DateTime TranDate { get; set; }

    [XmlElement("TXN_STATUS")]
    public string TxnStatus { get; set; }


    [XmlElement("REFUND_DATE")]
    public DateTime RefundDate { get; set; }

    [XmlElement("RESPONSE_CODE")]
    public string ResponseCode { get; set; }


    [XmlElement("RESPONSE_DESC")]
    public string ResponseDesc { get; set; }

    [XmlAttribute("MERCHANT_TRANID")]
    public string MerchantTranId { get; set; }

}

同样的方式你需要car as array的多个级别 Check this example for multilevel deserialization

答案 12 :(得分:1)

我的解决方案:

  1. 使用Edit > Past Special > Paste XML As Classes获取代码中的课程
  2. 尝试以下操作:创建该类的列表(List<class1&gt;),然后使用XmlSerializer将该列表序列化为xml文件。
  3. 现在,您只需将该文件的正文替换为您的数据,然后尝试deserialize
  4. 代码:

    StreamReader sr = new StreamReader(@"C:\Users\duongngh\Desktop\Newfolder\abc.txt");
    XmlSerializer xml = new XmlSerializer(typeof(Class1[]));
    var a = xml.Deserialize(sr);
    sr.Close();
    

    注意:您必须注意根名称,不要更改它。我的是“ArrayOfClass1”

答案 13 :(得分:1)

如何对XML文档进行反序列化的通用类

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Generic class to load any xml into a class
// used like this ...
// YourClassTypeHere InfoList = LoadXMLFileIntoClass<YourClassTypeHere>(xmlFile);

using System.IO;
using System.Xml.Serialization;

public static T LoadXMLFileIntoClass<T>(string xmlFile)
{
    T returnThis;
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    if (!FileAndIO.FileExists(xmlFile))
    {
        Console.WriteLine("FileDoesNotExistError {0}", xmlFile);
    }
    returnThis = (T)serializer.Deserialize(new StreamReader(xmlFile));
    return (T)returnThis;
}

这部分可能是,也可能不是必要的。在Visual Studio中打开XML文档,右键单击XML,选择属性。然后选择您的架构文件。

答案 14 :(得分:1)

您可以为您更改汽车汽车属性的一个属性,从XmlArrayItem到XmlElment。也就是说,来自

[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
    [XmlArrayItem(typeof(Car))]
    public Car[] Car { get; set; }
}

[System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
public class Cars
{
    [XmlElement("Car")]
    public Car[] Car { get; set; }
}

答案 15 :(得分:1)

async public static Task<JObject> XMLtoNETAsync(XmlDocument ToConvert)
        {
            //Van XML naar JSON
            string jsonText = await Task.Run(() => JsonConvert.SerializeXmlNode(ToConvert));
            //Van JSON naar .net object
            var o = await Task.Run(() => JObject.Parse(jsonText));
            return o;
        }

答案 16 :(得分:1)

如果您使用xsd.exe创建xsd文件时收到错误,请使用上面提到的XmlSchemaInference类on msdn。这是一个单元测试来证明:

using System.Xml;
using System.Xml.Schema;

[TestMethod]
public void GenerateXsdFromXmlTest()
{
    string folder = @"C:\mydir\mydata\xmlToCSharp";
    XmlReader reader = XmlReader.Create(folder + "\some_xml.xml");
    XmlSchemaSet schemaSet = new XmlSchemaSet();
    XmlSchemaInference schema = new XmlSchemaInference();

    schemaSet = schema.InferSchema(reader);


    foreach (XmlSchema s in schemaSet.Schemas())
    {
        XmlWriter xsdFile = new XmlTextWriter(folder + "\some_xsd.xsd", System.Text.Encoding.UTF8);
        s.Write(xsdFile);
        xsdFile.Close();
    }
}

// now from the visual studio command line type: xsd some_xsd.xsd /classes