如何生产出的属性,而不是XML节点使用MvcContrib的XmlResult? [英] How to produce XML with attributes rather than nodes, using MvcContrib's XmlResult?

查看:50
本文介绍了如何生产出的属性,而不是XML节点使用MvcContrib的XmlResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成在C#中类型的XML输出​​。我使用MvcContrib XmlResult类,我已经包含了链接的下面。

I'm trying to generate an XML output from a type in C#. I'm Using the MvcContrib XmlResult class, which I've included a link to below.

作为一个简单的测试,看看如果我能得到它的工作,我用下面的code:

As a simple test to see if I could get it working, I'm using the following code:

var topTen = new TopTen
{
    PerformanceTo = DateTime.Now,
    ShareClasses = new List<TopTenShareClass>()
                        {
                            new TopTenShareClass{Id = 1, Name = "a"},
                            new TopTenShareClass{Id = 2, Name = "b"},
                            new TopTenShareClass{Id = 3, Name = "c"}

                        }
};

return new XmlResult(topTen);

(与定义为2种类型:)

(with the 2 types defined as:)

public class TopTen
{
    public DateTime PerformanceTo { get; set; }
    public List<TopTenShareClass> ShareClasses { get; set; }
}

public class TopTenShareClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}

要产生这种输出XML

to produce this output XML

<?xml version="1.0" encoding="utf-8"?>
<TopTen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PerformanceTo>2011-02-22T10:56:41.3094548+00:00</PerformanceTo>
  <ShareClasses>
    <TopTenShareClass>
      <Id>1</Id>
      <Name>a</Name>
    </TopTenShareClass>
    <TopTenShareClass>
      <Id>2</Id>
      <Name>b</Name>
    </TopTenShareClass>
    <TopTenShareClass>
      <Id>3</Id>
      <Name>c</Name>
    </TopTenShareClass>
  </ShareClasses>
</TopTen>

我不知道是否有可能有ID&放大器;姓名标签显示为 TopTenShareClass 节点属性,而不是节点本身?理想情况下,XML会是这样:

I'm wondering if it's possible to have the ID & Name tags appear as attributes in the TopTenShareClass node, rather than nodes themselves? Ideally the XML would be like:

<?xml version="1.0" encoding="utf-8"?>
<TopTen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PerformanceTo>2011-02-22T10:56:41.3094548+00:00</PerformanceTo>
  <ShareClasses>
    <TopTenShareClass Id=1 Name='a'></TopTenShareClass>
    <TopTenShareClass Id=2 Name='b'></TopTenShareClass>
    <TopTenShareClass Id=3 Name='c'></TopTenShareClass>
  </ShareClasses>
</TopTen>

有关参考,XmlResult定义可以在这里找到:<一href=\"http://mvccontrib.$c$cplex.com/SourceControl/changeset/view/5f542a2e51e9#src%2fMVCContrib%2fActionResults%2fXmlResult.cs\" rel=\"nofollow\">http://mvccontrib.$c$cplex.com/SourceControl/changeset/view/5f542a2e51e9#src%2fMVCContrib%2fActionResults%2fXmlResult.cs

For reference, the XmlResult definition is available here: http://mvccontrib.codeplex.com/SourceControl/changeset/view/5f542a2e51e9#src%2fMVCContrib%2fActionResults%2fXmlResult.cs

推荐答案

您可以控制​​XML序列化进程的使用属性的:

You could control the XML serialization process using attributes:

public class TopTenShareClass
{
    [XmlAttribute]
    public int Id { get; set; }

    [XmlAttribute]
    public string Name { get; set; }
}

这篇关于如何生产出的属性,而不是XML节点使用MvcContrib的XmlResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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