反序列化XML数组,其中根是数组和元素不要按照约定 [英] Deserialize XML Array Where Root is Array and Elements Dont Follow Conventions

查看:251
本文介绍了反序列化XML数组,其中根是数组和元素不要按照约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的XML是由外部源提供的,所以我没有轻松重新格式化的能力。我想用,而不必编写知道如何将XML和实体格式化LINQ查询XML属性在我的实体。下面是一个例子:

The XML I am getting is provided by an outside source so I don't have the ability to easily reformat it. I would like to use xml attributes on my entities instead of having to write a linq query that knows how the XML and entity is formatted. Here is an example:

<?xml version="1.0"?>
<TERMS>
    <TERM>
        <ID>2013-2</ID>
        <DESC>Spring 2013</DESC>
    </TERM>
    <TERM>
        <ID>2013-3</ID>
        <DESC>Summer 2013 Jun&amp;Jul</DESC>
    </TERM>
</TERMS>

我知道XmlSerializer的预计,而不是条款,例如ArrayOfTerm,但我可以调整我的实体使用不同的元素名称的XML属性是这样:

I know the the XMLSerializer expects ArrayOfTerm instead of TERMS for example, but that I can tweak my entity to use a different element name with the xml attributes such as this:

public class TermData
{
    [XmlArray("TERMS")]
    [XmlArrayItem("TERM")]
    public List<Term> terms;
}

public class Term
{
    [XmlElement("ID")]
    public string id;

    [XmlElement("DESC")]
    public string desc;
}

和我反序列化的数据,像这样:

and I am deserializing the data like so:

TermData data;

XmlSerializer serializer = new XmlSerializer(typeof(TermData));
using (StringReader reader = new StringReader(xml))
{
   data = (TermData)serializer.Deserialize(reader);
}

return View(data.terms);

我现在面临的问题是,条款是根和数组本身。如果XML是有事实并非阵列的根元素,我可以编辑我的TermData类像这样,它会反序列化正确(已测试)。

The problem I am facing is that TERMS is the root and the array itself. If the XML were to have a root element that was not the array, I could edit my TermData class like so and it would deserialize correctly (already tested).

[XmlRoot("ROOT")]
public class TermData
{
    [XmlArray("TERMS")]
    [XmlArrayItem("TERM")]
    public List<Term> terms;
}

请注意,使用术语的XMLRoot不起作用。现在,我的code抛出

Note that using TERMS as the XMLRoot does not work. Right now, my code is throwing

 InvalidOperationException: There is an error in XML document (2,2).
 InnerException: "<TERMS xmlns=" was not expected.

这将导致我相信XML格式不正确,但是从我的理解我给的例子是完全有效的XML。

This would lead me to believe that the XML is not formatted correctly, but from my understanding the example I gave is perfectly valid XML.

这都将是微不足道的,如果我可以编辑XML源,但有可能会吨这样的其他答复,我需要能够弯曲不管是什么我可能会。我想要确认是XmlSerializer的是否能支持这种类型的XML结构。我测试过刚才的一切,并不能让它不反序列编辑XML。这也将是方便,如果我没有定义一个包装类(TermData)来保存列表,但这似乎只是工作,如果XML遵循的命名约定的串行器(ArrayOfTerm等)。

This would all be trivial if I could edit the source xml, but there could be tons of other responses like this and I need to be able to flex for whatever I might get. What I'm trying to confirm is whether or not the XMLSerializer can support this type of XML structure. I've tested just about everything and can't get it deserialize without editing the XML. It would also be convenient if I didn't have to define a wrapper class (TermData) to hold the list, but this seems to only work if the xml follows the naming conventions for the serializer (ArrayOfTerm, etc).

推荐答案

也许你可以试试:

[XmlRoot("TERMS")]
public class TermData
{
    public TermData()
     {
       terms = new List<Term>();
     }

    [XmlElement("TERM")]
    public List<Term> terms{get;set;}
}

public class Term
{
    [XmlElement("ID")]
    public string id{get;set;}

    [XmlElement("DESC")]
    public string desc{get;set;}
}

希望这会帮助,

Hope this will help,

这篇关于反序列化XML数组,其中根是数组和元素不要按照约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆