基于我的 c# 类生成 xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14953311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:31:28  来源:igfitidea点击:

generate xml files based on my c# classes

c#xmlc#-4.0xml-serialization

提问by Prashant

i have xml file that i need to update each and every time as per new client requirement. most of the time xml is not proper because of manual updation of xml file. I am thinking to write a program (web/windows) where proper validation is provided. and based on the input from ui I will going to create xml file. below is my sample xml file.

我有 xml 文件,我需要根据新的客户要求每次更新。由于手动更新 xml 文件,大多数时候 xml 不正确。我正在考虑编写一个提供适当验证的程序(web/windows)。并基于来自 ui 的输入,我将创建 xml 文件。下面是我的示例 xml 文件。

<community>
  <author>xxx xxx</author>
  <communityid>000</communityid>
  <name>name of the community</name>

<addresses>
        <registeredaddress>
          <addressline1>xxx</addressline1>
          <addressline2>xxx</addressline2>
          <addressline3>xxx</addressline3>
          <city>xxx</city>
          <county>xx</county>
          <postcode>0000-000</postcode>
          <country>xxx</country>
        </registeredaddress>
        <tradingaddress>
          <addressline1>xxx</addressline1>
          <addressline2>xxx</addressline2>
          <addressline3>xxx</addressline3>
          <city>xxx</city>
          <county>xx</county>
          <postcode>0000-000</postcode>
          <country>xxx</country>
        </tradingaddress>
      </addresses>


<community>

can any one help me what will be the best approach for this?

任何人都可以帮助我什么是最好的方法吗?

采纳答案by Sergey Berezovskiy

Create following classes to hold your data and validate it:

创建以下类来保存您的数据并对其进行验证:

public class Community
{
    public string Author { get; set; }
    public int CommunityId { get; set; }
    public string Name { get; set; }
    [XmlArray]
    [XmlArrayItem(typeof(RegisteredAddress))]
    [XmlArrayItem(typeof(TradingAddress))]
    public List<Address> Addresses { get; set; }
}

public class Address
{
    private string _postCode;

    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string City { get; set; }
    public string Country { get; set; }

    public string PostCode
    {
        get { return _postCode; }
        set {
            // validate post code e.g. with RegEx
            _postCode = value; 
        }
    }
}

public class RegisteredAddress : Address { }
public class TradingAddress : Address { }

And serialize that instance of community class to xml:

并将该社区类的实例序列化为 xml:

Community community = new Community {
    Author = "xxx xxx",
    CommunityId = 0,
    Name = "name of community",
    Addresses = new List<Address> {
        new RegisteredAddress {
            AddressLine1 = "xxx",
            AddressLine2 = "xxx",
            AddressLine3 = "xxx",
            City = "xx",
            Country = "xxxx",
            PostCode = "0000-00"
        },
        new TradingAddress {
            AddressLine1 = "zz",
            AddressLine2 = "xxx"
        }
    }
};

XmlSerializer serializer = new XmlSerializer(typeof(Community));
serializer.Serialize(File.Create("file.xml"), community);

I think a little googling will help you to understand how to deserialize community object back from file.

我认为一点谷歌搜索将帮助您了解如何从文件中反序列化社区对象。