基于我的c类生成xml文件
本文关键字:xml 文件 于我的 我的 | 更新日期: 2023-09-27 18:00:08
我有一个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>
有人能帮我什么是最好的方法吗?
创建以下类来保存数据并对其进行验证:
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:
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);
我认为一点谷歌搜索将帮助您了解如何从文件反序列化社区对象。
这是一个老话题,但我想分享一下。您上面提供的xml无效,所以我使用下面的xml作为示例。
-
首先,按照您希望的样子构建xml。假设它看起来是这样的:
<note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
-
复制您的xml代码
-
转到Visual Studio
-
创建一个类文件并清除其中的代码
-
然后在菜单上,单击
Edit'Paste Special'Paste Xml as Classes
Visual studio将生成整个类。
Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class note
{
private string toField;
private string fromField;
private string headingField;
private string bodyField;
/// <remarks/>
public string to
{
get
{
return this.toField;
}
set
{
this.toField = value;
}
}
/// <remarks/>
public string from
{
get
{
return this.fromField;
}
set
{
this.fromField = value;
}
}
/// <remarks/>
public string heading
{
get
{
return this.headingField;
}
set
{
this.headingField = value;
}
}
/// <remarks/>
public string body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}