简体   繁体   English

C#中的XML元素和属性等效项是什么?

[英]What are XML element and attribute equivalents in C#?

I am trying to define C# objects based on this XML: 我试图基于此XML定义C#对象:

<UPDs LUPD="86">
  <UPD ID="106">
    <ER R="CREn">
      <NU UID="1928456" />
      <NU UID="1886294" />
      <M>
        <uN>bob · </uN>
        <mO>fine :D</mO>
      </M>

So far I have: 到目前为止,我有:

public class UDPCollection    
{
    List<UDP> UDPs; 

    public UDPCollection()
    {
        UDPs = new List<UDP>();
    }
}

public class UDP
{
    public int Id;
    public List<ER> ERs;
    public UDP(int id, List<ER> ers)
    {
        Id = id;
        ERs = ers;
    }
}

public class ER
{
    public string LanguageR;

    public ER(string languager)
    {
        LanguageR = languager;
    }
}

My questions: What do elements map to in C#? 我的问题:在C#中元素映射到什么? Classes? 上课吗 What do attributes map to? 属性映射到什么? Properties? 属性? Am I going about this the correct way? 我要用正确的方法吗?

Use the XmlSerializer class and the XmlRoot , XmlElement and XmlAttribute attributes. 使用XmlSerializer类和XmlRootXmlElementXmlAttribute属性。 For example: 例如:

using System.Xml.Serialization;

...

[XmlRoot("UPDs")]
public class UDPCollection
{
    // XmlSerializer cannot serialize List. Changed to array.
    [XmlElement("UPD")]
    public UDP[] UDPs { get; set; }

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

    public UDPCollection()
    {
        // Do nothing
    }
}

[XmlRoot("UPD")]
public class UDP
{
    [XmlAttribute("ID")]
    public int Id { get; set; }

    [XmlElement("ER")]

    public ER[] ERs { get; set; }

    // Need a parameterless or default constructor.
    // for serialization. Other constructors are
    // unaffected.
    public UDP()
    {
    }

    // Rest of class
}

[XmlRoot("ER")]
public class ER
{
    [XmlAttribute("R")]
    public string LanguageR { get; set; }

    // Need a parameterless or default constructor.
    // for serialization. Other constructors are
    // unaffected.
    public ER()
    {
    }

    // Rest of class
}

The code to write out the XML is: 编写XML的代码是:

using System.Xml.Serialization;

...

// Output the XML to this stream
Stream stream;

// Create a test object
UDPCollection udpCollection = new UDPCollection();
udpCollection.LUPD = 86;
udpCollection.UDPs = new []
{
    new UDP() { Id= 106, ERs = new [] { new ER() { LanguageR = "CREn" }}}
};

// Serialize the object
XmlSerializer xmlSerializer = new XmlSerializer(typeof(UDPCollection));
xmlSerializer.Serialize(stream, udpCollection);

Not that the XmlSerializer adds additional namepsaces but it can parse XML without them if needed. 并非XmlSerializer添加了其他名称空间,但是如果需要,它可以解析XML而无需它们。 The output of the above is: 上面的输出是:

<?xml version="1.0"?>
<UPDs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" LUPD="86">
    <UPD ID="106">
       <ER R="CREn" />
    </UPD>
</UPDs>

Use the Deserialize() method to parse it from XML into an object. 使用Deserialize()方法将其从XML解析为一个对象。

XML elements and attributes don't necessarily map to anything in particular in C#. XML元素和属性不一定映射到C#中的任何东西。 You can make them map to classes and properties if you want, but it's not required. 您可以根据需要使它们映射到类和属性,但这不是必需的。

That said, if you want to map your existing XML to some sort of C# data structures, the way you're doing it seems reasonable - I'd just recommend replacing your public fields with actual properties, and maybe making the list properties a less specific type - say, IEnumerable, or ICollection, or IList if they really need to be in order. 就是说,如果您想将现有的XML映射到某种C#数据结构,那么您这样做的方式似乎是合理的-我只建议您用实际属性替换您的公共字段,也许可以减少列表属性特定类型-例如IEnumerable,ICollection或IList(如果确实需要按顺序排列)。

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

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