根据我的C#类生成xml文件 [英] generate xml files based on my c# classes

查看:84
本文介绍了根据我的C#类生成xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有xml文件,我需要根据新客户端的要求每次进行更新。
多数情况下,由于手动更新xml文件,xml不正确。
我正在考虑编写一个提供适当验证的程序(网络/窗口)。
,根据ui的输入,我将创建xml文件。下面的
是我的示例xml文件。

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.

<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?

推荐答案

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

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 { }

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

And serialize that instance of community class to 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);

我认为略微使用Google搜索可以帮助您了解如何从文件中反序列化社区对象。

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

这篇关于根据我的C#类生成xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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