如何添加属性为C#XML序列化 [英] How to add attributes for C# XML Serialization

查看:408
本文介绍了如何添加属性为C#XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有序列化和对象的问题,我可以得到它除了在那里我有一个需要一个值和一个属性的元素创建所有正确的输出。这里是所需的输出:

I am having an issue with serializing and object, I can get it to create all the correct outputs except for where i have an Element that needs a value and an attribute. Here is the required output:

<Root>
  <Method>Retrieve</Method>
  <Options>
    <Filter>
      <Times>
        <TimeFrom>2009-06-17</TimeFrom>
      </Times>
      <Document type="word">document name</Document>
    </Filter>
  </Options>
</AdCourierAPI>

我可以建立这一切,但不能找到一种方法来设置文档类型属性,这里是对象类的一个片段

I can build all of it but can not find a way to set the Document type attribute, here is a segment of the object class

[XmlRoot("Root"), Serializable]    
public class Root    
{    
    [XmlElement("Method")]    
    public string method="RetrieveApplications";    
    [XmlElement("Options")]    
    public _Options Options;    
}    
public class _Options    
{
    [XmlElement("Filter")]    
    public _Filter Filter;    
}
public class _Filter    
{
    [XmlElement("Times")]    
    public _Times Times;    
    [XmlElement("Documents")]    
    public string Documents;    
}

这给了我:

<Document>document name</Document>

而不是:

<Document type="word">document name</Document>

但我不能找到一个方法来解决这个问题,请大家指教。

but I can not find a way to correct this, please advise.

感谢

推荐答案

你在哪儿了键入存储?

通常情况下,你可以有这样的:

Normally you could have something like:

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


public class _Filter    
{
    [XmlElement("Times")]    
    public _Times Times;    
    [XmlElement("Document")]    
    public Document Document;    
}

这篇关于如何添加属性为C#XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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