如何使用工具从XML生成类,以便在Silverlight中序列化?
从复杂XML生成类以在Silverlight中反序列化的最佳方法是什么? 在.NET中,您可以使用Visual Studio中的xsd.exe 你会得到这样的东西吗如何使用工具从XML生成类,以便在Silverlight中序列化?,xml,silverlight,serialization,Xml,Silverlight,Serialization,从复杂XML生成类以在Silverlight中反序列化的最佳方法是什么? 在.NET中,您可以使用Visual Studio中的xsd.exe 你会得到这样的东西吗 using System.Xml.Serialization; /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] // this is unknown in SILVERLIGHT [System
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
// this is unknown in SILVERLIGHT [System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
//this is unknown in SILVERLIGHT [System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Attest {
private AttestBet[] betField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Bet")]
public AttestBet[] Bet {
get {
return this.betField;
}
set {
this.betField = value;
}
}
}
当我对它进行注释,并使用没有这两行代码的代码时,它在一个简单的示例中起到了一定的作用。有没有更好的工具来生成在Silverlight中使用的类?你说得对,没有人真的这么做,只需解析XML文件就更容易了。但是,如果您希望将控件直接绑定到XML文件,并希望进行良好的序列化/愿望化,则可以使用下面的代码。通常,我仅在处理WCF无法处理的大数据(即每次调用50-600MB)时使用此方法 创建一个名为XmlNodeItem的新类。该类表示XML文件中的一个单独节点
public class XmlNodeItem
{
[XmlAttribute( AttributeName = "Bet" )]
public string Bet
{
get;
set;
}
[XmlAttribute( AttributeName = "SomeOtherStuff" )]
public string SomeOtherStuff
{
get;
set;
}
}
然后创建另一个名为XmlNodeItemList的类,该类派生自XmlNodeItem的ObservableCollection
[XmlRoot( ElementName = "Items" )]
public class XmlNodeItemList : ObservableCollection<XmlNodeItem>
{
public void AddRange( IEnumerable<XmlNodeItem> range )
{
foreach ( XmlNodeItem node in range )
{
this.Add( node );
}
}
}
你说得对,没有人真正做到这一点,只需解析XML文件就更容易了。但是,如果您希望将控件直接绑定到XML文件,并希望进行良好的序列化/愿望化,则可以使用下面的代码。通常,我仅在处理WCF无法处理的大数据(即每次调用50-600MB)时使用此方法 创建一个名为XmlNodeItem的新类。该类表示XML文件中的一个单独节点
public class XmlNodeItem
{
[XmlAttribute( AttributeName = "Bet" )]
public string Bet
{
get;
set;
}
[XmlAttribute( AttributeName = "SomeOtherStuff" )]
public string SomeOtherStuff
{
get;
set;
}
}
然后创建另一个名为XmlNodeItemList的类,该类派生自XmlNodeItem的ObservableCollection
[XmlRoot( ElementName = "Items" )]
public class XmlNodeItemList : ObservableCollection<XmlNodeItem>
{
public void AddRange( IEnumerable<XmlNodeItem> range )
{
foreach ( XmlNodeItem node in range )
{
this.Add( node );
}
}
}
我在Silverlight项目中使用了xsd。我只是在生成的代码中进行了快速搜索和替换,以删除所有不受支持的属性。每次更改模式时都必须执行此手动步骤,这很烦人,但它是有效的。我在Silverlight项目中使用了xsd。我只是在生成的代码中进行了快速搜索和替换,以删除所有不受支持的属性。每次更改模式时都必须执行此手动步骤,这很烦人,但它是有效的。