使用ASP.NET中的实例值自定义XML标记

问题描述 投票:0回答:1

我正在研究此ASP.NET Api,其中通过GET请求获取数据,格式为XML。

现在,所有xml节点都是使用代码元素(类名,属性名等)生成的。如果需要,我可以使用[XmlType(TypeName =“ DesiredName”)]之类的属性来更改这些节点名称,但是仍然可以将其与我在代码中使用的名称进行一对一映射。 >

我有一个特定的对象类型,我想使用实际的运行时值来格式化,而不是使用编译时符号。

以下是课程:

public class RootClass
{
    public string ID { get; set; }
    public List<Property> Properties { get; set; }
}

public class Property
{
    public string PropertyType { get; set; }
    // Other Fields
}

假设我有几个属性,它们的属性类型为“ Type1”,“ Type2”,“ Type3”

我想要的Xml看起来像这样:

<RootClass>
    <ID>23</ID>
    <Properties>
        <Type1>
            <Property>
                // Other Fields
            </Property>
            <Property>
                // Other Fields
            </Property>
        </Type1>
        <Type2>
            <Property>
                // Other Fields
            </Property>
        </Type2>
    </Properties>
</RootType>

如您所见,将根据值而非模式来命名节点之一。从模型到DTO映射时,我可以通过代码轻松地进行分组,并且可以完全控制DTO的类,因此可以根据需要更改它们,以获取期望的XML。

首先,这有可能吗?

如果是这样,我的类结构(和属性)应该如何实现此目的(列表字典?自定义集合?)>

如果不是,我还需要其他哪些选择来实现与此目标尽可能接近的东西?

我正在研究此ASP.NET Api,其中通过GET请求获取数据,格式为XML。现在,所有xml节点都是使用代码元素(类名,属性名等)生成的。我...

c# asp.net xml dto
1个回答
0
投票

尝试Xml Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            RootClass root = new RootClass()
            {
                ID = "23",
                Properties = new List<Property>() {
                    new Property() { PropertyType = "Type1", Properties = new List<string>() { "A", "B", "C"} },
                    new Property() { PropertyType = "Type2", Properties = new List<string>() { "A", "B", "C"} }
                }
            };

            XElement rootClass = new XElement("RootClass", new object[] {
                new XElement("ID", root.ID),
                new XElement("Properties")
            });

            XElement properties = rootClass.Element("Properties");
            int count = 1;
            foreach (Property property in root.Properties)
            {
                XElement type = new XElement(property.PropertyType);
                properties.Add(type);
                foreach (string p in property.Properties)
                {
                    XElement strProperty = new XElement("Property", new object[] {
                        new XElement(p, count++)
                    });
                    type.Add(strProperty);
                }
            }

            rootClass.Save(FILENAME);
        }
    }
    public class RootClass
    {
        public string ID { get; set; }
        public List<Property> Properties { get; set; }
    }

    public class Property
    {
        public string PropertyType { get; set; }
        public List<string> Properties { get; set; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.