简体   繁体   English

在根为数组且元素不遵循约定的情况下反序列化XML数组

[英]Deserialize XML Array Where Root is Array and Elements Dont Follow Conventions

The XML I am getting is provided by an outside source so I don't have the ability to easily reformat it. 我要获取的XML由外部来源提供,因此我无法轻松地对其进行重新格式化。 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. 我想在实体上使用xml属性,而不必编写知道XML和实体格式的linq查询。 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>

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: 我知道XMLSerializer期望使用ArrayOfTerm而不是TERMS,例如,但是我可以调整实体以使用具有xml属性的其他元素名称,例如:

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);

The problem I am facing is that TERMS is the root and the array itself. 我面临的问题是TERMS是根目录和数组本身。 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). 如果XML的根元素不是数组,那么我可以像这样编辑TermData类,它将正确反序列化(已经过测试)。

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

Note that using TERMS as the XMLRoot does not work. 请注意,将TERMS用作XMLRoot无效。 Right now, my code is throwing 现在,我的代码正在抛出

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

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的格式不正确,但是据我了解,我给出的示例是完全有效的XML。

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. 如果我可以编辑源xml,这将是微不足道的,但是可能会有大量其他类似的响应,并且我需要能够为自己可能得到的一切做出灵活的选择。 What I'm trying to confirm is whether or not the XMLSerializer can support this type of XML structure. 我要确认的是XMLSerializer是否可以支持这种类型的XML结构。 I've tested just about everything and can't get it deserialize without editing the XML. 我已经测试了几乎所有内容,并且不编辑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). 如果我不必定义包装类(TermData)来保存列表,这也将很方便,但这似乎仅在xml遵循序列化程序的命名约定(ArrayOfTerm等)的情况下起作用。

Maybe you can try : 也许您可以尝试:

[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, 希望这会有所帮助,

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

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