0
我已经使用xsd.exe生成名为“FacturaE”的代理类。使用XSD中的自动生成的代理类(.Net C#4.0)
我可以实例化这个类,填充它的信息,并保存到XML与XmlSerializer。
但是,给定一个XML格式的文件,我怎样才能将它读入“FacturaE”?
感谢
我已经使用xsd.exe生成名为“FacturaE”的代理类。使用XSD中的自动生成的代理类(.Net C#4.0)
我可以实例化这个类,填充它的信息,并保存到XML与XmlSerializer。
但是,给定一个XML格式的文件,我怎样才能将它读入“FacturaE”?
感谢
至于XSD.EXE的替代品,Xsd2Code是一个更全功能类生成。它可以向生成的类添加方法,以便从Xml序列化和反序列化。
以下是由Xsd2Code产生的序列化/反序列化方法的一个例子:
/// <summary>
/// Serializes current EntityBase object into an XML document
/// </summary>
// <returns>string XML value</returns>
public virtual string Serialize() {
System.IO.StreamReader streamReader = null;
System.IO.MemoryStream memoryStream = null;
try {
memoryStream = new System.IO.MemoryStream();
Serializer.Serialize(memoryStream, this);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
streamReader = new System.IO.StreamReader(memoryStream);
return streamReader.ReadToEnd();
}
finally {
if (streamReader != null) {
streamReader.Dispose();
}
if (memoryStream != null) {
memoryStream.Dispose();
}
}
}
/// <summary>
/// Deserializes workflow markup into an EntityBase object
/// </summary>
// <param name="xml">string workflow markup to deserialize</param>
// <param name="obj">Output EntityBase object</param>
// <param name="exception">output Exception value if deserialize failed</param>
// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
public static bool Deserialize(string xml, out T obj, out System.Exception exception) {
exception = null;
obj = default(T);
try {
obj = Deserialize(xml);
return true;
}
catch (System.Exception ex) {
exception = ex;
return false;
}
}
public static bool Deserialize(string xml, out T obj) {
System.Exception exception = null;
return Deserialize(xml, out obj, out exception);
}
public static T Deserialize(string xml) {
System.IO.StringReader stringReader = null;
try {
stringReader = new System.IO.StringReader(xml);
return ((T)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
finally {
if (stringReader != null) {
stringReader.Dispose();
}
}
}