具有相同名称的Deserialzie XML Elements [英] Deserialzie XML Elements with the same name

查看:59
本文介绍了具有相同名称的Deserialzie XML Elements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML文档:

I have the following XML Document:

<?xml version="1.0" encoding="utf-8"?>
<csApplication>
<csCollDocuments collection="textValue1" type="dynamic">
<csDocument name="textValue1" master="masterValue" res_loc="location">
<variable name="variableName1" type="string">VariableValue1</variable>
<variable name="variableName2" type="string">VariableValue2</variable>
<variable name="variableName3" type="string">VariableValue3</variable>
</csDocument>
</csCollDocuments>
</csApplication>


我创建了下面的类进行反序列化,但是我只得到第一个Variable元素.我将如何获得所有这些?


I''ve created the below class to deserialize to, but I''m only getting the first Variable element. How would I go about getting all of them?

[XmlRoot("csApplication")]
public class JobTicketDetails
{
    [XmlElement("csCollDocuments")]
    public Documents Documents { get; set; }
}

public class Documents
{
    [XmlAttribute("collection")]
    public string Collection { get; set; }

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlElement("csDocument")]
    public Document Document { get; set; }
}

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

    [XmlAttribute("master")]
    public string Master { get; set; }

    [XmlAttribute("res_loc")]
    public string ResLoc { get; set; }

    private List<Variable> _Variables = new List<Variable>();
    [XmlElement("variable")]
    public Variable Variable
    {
        get { return _Variables[0]; }

        set { _Variables.Add(value); }
    }
}

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

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlText]
    public string Text { get; set; }
}

推荐答案

替换此

Replace this

private List<Variable> _Variables = new List<Variable>();
[XmlElement("variable")]
public Variable Variable
{
    get { return _Variables[0]; }

    set { _Variables.Add(value); }
}



通过这个



by this

[XmlElement("variable")]
public List<Variable> Variables { get; set; }

public Document()
{
    Variables = new List<Variable>();
}



原因:如果在父XML列表元素"中有一个包围的XML元素列表,则可以定义该列表元素的C#属性,并通过XmlElement属性修饰该属性. /> 如果您有一个封闭的XML列表元素,您仍然可以定义C#列表属性,但使用该属性上的XmlArray和XmlArrayItem属性对其进行修饰.



Reason: if you have a list of XML elements that are not enclosed in a parent XML "list element", you define a C# property of that list element and decorate that property by the XmlElement attribute.
If you have an enclosing XML list element, you still define the C# list property, but decorate it with the XmlArray and XmlArrayItem attribute on that property.


当我查看您的预期名称时,觉得您的命名有点人工.例如,为什么您更喜欢元素csCollDocuments而不是Documents.首先想到的是:您正在尝试引入该应用程序特有的概念,可能是为了避免与其他Documents的命名冲突,因为所有单词都被广泛使用.这看起来像是呼吁使用更全面的表达工具,例如XML名称空间.请参阅:
http://en.wikipedia.org/wiki/XML_namespace [ http://www.w3.org/TR/2006/REC-xml-names11-20060816 / [ ^ ].

代码中的所有内容,定义数据模型的样式(我喜欢),结构—告诉我,您可能需要不同的,更一致的方法,称为 Data Contract .

请参阅:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractserializer.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx [ ^ ].

这种方法最灵活,同时最容易开发,并且最不介入.
请在我倡导这种方法的地方查看我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [ http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datamemberattribute.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractattribute.aspx [ ^ ].



为了回应后续讨论,以防万一:

即使数据合同可以完全解决您的问题,但由于外部系统的某些特殊性,您仍可能会遇到持久数据格式可能需要深度定制的情况.在这种困难的情况下,始终可以使用最灵活的XML解析和生成类System.Xml.XmlTextWriterSystem.Xml.XmlTextReader来解决该问题.这是最快的读取方法,尤其是您需要跳过一些数据.

请参阅:
http://msdn.microsoft.com/en-us/library/system.xml. xmlwriter.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ].

—SA
When I look at your intended names, I feel that your naming is somewhat artificial. For example, why would you prefer the element csCollDocuments to just Documents. First idea which comes to mind is: you are trying to introduce a notion unique to the application, possibly to avoid naming conflict with some other Documents, as all the words are widely used. That looks like a call for much more comprehensive expression tool, such as XML namespaces. Please see:
http://en.wikipedia.org/wiki/XML_namespace[^],
http://www.w3.org/TR/2006/REC-xml-names11-20060816/[^].

Everything in your code, the style you define your data model (which I like), the structure — tells me that you probably need different, much more consistent approach called Data Contract.

Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx[^].

This approach is most flexible and at the same time easiest for development and most non-intrusive.
Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

There are many important features in this approach, but in particular, you can use different attribute to modify naming, both name itself via the Name of the [DataMember] attribute and Namespace of the [DataContract] attribute. Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx[^].



In response to the follow-up discussion and just in case:

Even though the Data Contract could totally solve your problem, you might face the situation where the persistent data format might need some deep customization, due the some peculiarities of the external system. In such difficult cases, the solution is always possible using most flexible XML parsing and generation classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.

Please see:
http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].

—SA


这篇关于具有相同名称的Deserialzie XML Elements的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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