简体   繁体   English

C#类属性的XML属性值

[英]XML attribute value to C# class property

I have a xml file that I want to map to a C# class 我有一个要映射到C#类的xml文件

The xml looks like this: xml看起来像这样:

<root>
    <table>
       <fields>
           <field name="createdby">Thomas</field>
           <field name="id">123</field>
           <field name="Title">New Article</field>
       </fields>
    </table>
</root>

And I have created some classes to map this, not sure if this is the shortest way to do that: 我已经创建了一些类来映射它,不确定这是否是最短的方法:

Root: 根:

    [XmlRoot(ElementName = "root")]
    public class Root
    {
        [XmlElement(ElementName = "table")]
        public Table Table { get; set; }

    }

Table: 表:

    [XmlRoot(ElementName = "table")]
    public class Table
    {
        [XmlElement(ElementName = "fields")]
        public Fields Fields { get; set; }
        //[XmlAttribute(AttributeName = "name")]
        //public string Name { get; set; }
        //[XmlElement(ElementName = "table")]
        //public List<Table> TableList { get; set; }
        }

Fields: 领域:

    [XmlRoot(ElementName = "fields")]
    public class Fields
    {
        [XmlElement(ElementName = "field")]
        public List<Field> Field { get; set; }
    }

Field: 领域:

    [XmlRoot(ElementName = "field")]
    public class Field
    {
        //Would like to the the createdby name here "Thomas"
        [XmlAttribute(AttributeName="createdby")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName="id")]
        public int Id { get; set; }
        [XmlAttribute(AttributeName="title")]
        public string Title { get; set; }
    }

is this the right approach ?? 这是正确的方法吗?

Hope someone can help and maybe find a clean way.. :P 希望有人可以提供帮助,也许找到一种干净的方法。

The goal is to have a class called 目标是开设一个名为

public class Article
    {
        public string Name { get; set; }
        public string Id { get; set; }
        public string CreatedBy { get; set; }

        public string Content { get; set; }
    }

Where I can map data values from the xml file to this. 我可以在其中将数据值从xml文件映射到此文件。 Something like this. 这样的事情。

Article article = new Article();
Article.CreatedBy = result.Table.Fields.Field.Createdby

as i understand you need a proper class to serialize to. 据我了解,您需要适当的类进行序列化。 here as example: 这里作为例子:

public partial class root
{
    public Table table {get;set;}
}
public partial class Table
{
    public Fields fields {get;set;}
}

public partial class Fields
{
    public Field field {get;set;}
}
public partial class Field
{
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {get;set;}
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {get;set;}
}

and code (there are different approaches) i made up as example: 和我编写的代码(有不同的方法)作为示例:

string xml = "<root><table><fields><field name=\"createdBy\">Thomas</field></fields></table></root>";
    XDocument x = XDocument.Parse(xml, LoadOptions.PreserveWhitespace);
    XmlSerializer ser = new XmlSerializer(typeof(root));
    XmlReader reader2 = x.CreateReader();
    root res = (root)ser.Deserialize(reader2);
    var author = res.table.fields.field;
    Console.WriteLine($"{author.name} : {author.Value}");

EDITED: you can add another filed to class (i added to root): 编辑:您可以将另一个字段添加到类(我添加到根目录):

public partial class root
{
    public Table table { get; set; }
    [XmlIgnoreAttribute]
    public string Author => table.fields.field.name + " " + table.fields.field.Value;
}

and now you can do what you want: 现在您可以做您想做的:

res.Author

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

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