gpt4 book ai didi

c# - XML 到 C# 类问题

转载 作者:数据小太阳 更新时间:2023-10-29 01:45:43 26 4
gpt4 key购买 nike

谁能帮帮我,我有这个 xml 片段

<?xml version="1.0" encoding="utf-8" ?>
<EmailConfiguration>
<DataBoxID>123</DataBoxID>
<DefaultSendToAddressCollection>
<EmailAddress>email@whereEver.com</EmailAddress>
</DefaultSendToAddressCollection>
</EmailConfiguration>

我想从中创建一个相应的 c# 类。在您说“只需使用 xsd.exe”之前,Xsd 的输出无法正确序列化和反序列化,因为它使用部分类生成类。

你能告诉我如何创建这个类....这是我采用的方法,但它不起作用。

public class EmailConfiguration
{
private string dataBoxID;

public string DataBoxID
{
get { return dataBoxID; }
set { dataBoxID = value; }
}

private DefaultSendToAddressCollectionClass defaultSendToAddressCollection;

public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection
{
get { return defaultSendToAddressCollection; }
set { defaultSendToAddressCollection = value; }
}
}

这是子类的类声明

public class DefaultSendToAddressCollectionClass
{
private string[] emailAddress;
public string[] EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
}

最佳答案

你用过VS2008的XSD吗?

这是我得到的输出:

c:>xsd email.xml
Writing file 'c:\email.xsd'

c:>xsd email.xsd /c /edb
Writing file 'c:\email.cs'

生成可序列化的输出:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class EmailConfiguration : object, System.ComponentModel.INotifyPropertyChanged {

private string dataBoxIDField;

private EmailConfigurationDefaultSendToAddressCollection[] defaultSendToAddressCollectionField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string DataBoxID {
get {
return this.dataBoxIDField;
}
set {
this.dataBoxIDField = value;
this.RaisePropertyChanged("DataBoxID");
}
}

关于c# - XML 到 C# 类问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1133015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com