简体   繁体   English

我如何反序列化这个xml?

[英]how do i deserialize this xml?

I need to deserialize the xml below. 我需要反序列化下面的xml。 I want to use xmlserializer because I am (more) familiar with it. 我想使用xmlserializer,因为我(更)熟悉它。 I believe this xml is not constructed correctly however I cannot change it. 我相信这个xml没有正确构造,但我不能改变它。 The below represents a list of category objects. 下面代表一个类别对象列表。 When I try to deserialize using 当我尝试反序列化时使用

xmlserializer(typeof(List<Category>))

I get this error: "categories xmlns='' is not expected" 我收到此错误:“类别xmlns =''不是预期的”

<?xml version="1.0" encoding="utf-8" ?>
<categories>
  <category id="16" name="Exports" parent_id="13"/>
  <category id="17" name="Imports" parent_id="13"/>
  <category id="3000" name="Income Payments & Receipts" parent_id="13"/>
  <category id="125" name="Trade Balance" parent_id="13"/>
  <category id="127" name="U.S. International Finance" parent_id="13"/>
</categories>

I don't mind making some kind of dummy class to deserilize these if that is what I have to do. 如果这是我必须做的事情,我不介意制作某种虚拟课程来去除它们。 Here is my Category Class 这是我的分类

[XmlType("category")]
 public class Category
 {


    [XmlAttribute("id")]
    public int ID { get; set; }

    [XmlAttribute("parent_id")]
    public int ParentID { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }
}

My code: 我的代码:

    XmlSerializer serializer = new XmlSerializer(typeof(List<Category>));
    StringReader reader = new StringReader(xml);
    List<Category> obj = null;

    using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader))
    {
        obj = (List<Category>)serializer.Deserialize(xmlReader);
    }
        return obj;

You can just pass in the XmlRootAttribute into the serializer for the "categories" part. 您只需将XmlRootAttribute传递给“类别”部分的序列化程序即可。

BUT... you must remove the "&" from your xml because its not valid 但是......你必须从xml中删除“&”,因为它无效

XmlSerializer serializer = new XmlSerializer(typeof(List<Category>), new XmlRootAttribute("categories"));

using (FileStream fileStream = new FileStream(@"C:\Test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
    var test = serializer.Deserialize(fileStream);
}

在此输入图像描述

Here is your method working with a String.Replace to sort out the "&" 这是使用String.Replace来排序“&”的方法

    private List<Category> GetCategories(string xmlData)
    {
        List<Category> obj = null;
        XmlSerializer serializer = new XmlSerializer(typeof(List<Category>), new XmlRootAttribute("categories"));
        StringReader reader = new StringReader(xmlData.Replace("&","&amp;"));
        using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader))
        {
            obj = (List<Category>)serializer.Deserialize(xmlReader);
        }
        return obj;
    }

Try to make a categories class that will contain your List<Category> like this: 尝试创建一个包含List<Category>的类别类,如下所示:

[XmlRoot("categories")]
public class Categories
{
    public Categories() 
    {
       Items = new List<User>();
    }

    [XmlElement("category")]
    public List<Category> Items {get;set;}
}

You can than create a serializer like this: 你可以创建一个像这样的序列化器:

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

Do you have an XSD that this XML should conform to? 你有一个这个XML应该符合的XSD吗? If so, you can generate the required code using: "xsd your.xsd /classes" 如果是这样,您可以使用以下命令生成所需的代码:“xsd your.xsd / classes”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM