将对象转换为 XML 字符串 [英] Convert an object to an XML string
问题描述
我有一个名为 WebserviceType
的类,我是从 XSD 文件的工具 xsd.exe 中得到的.
现在我想将 WebServiceType
对象的实例反序列化为字符串.我该怎么做?
MethodCheckType
对象有一个 WebServiceType
数组作为参数.
我的第一次尝试就像我序列化它一样:使用 XmlSerializer
和一个 StringWriter
(在序列化时我使用了 StringReader
).>
这是我序列化WebServiceType
对象的方法:
XmlSerializer serializer = new XmlSerializer(typeof(MethodCheckType));MethodCheckType 输出 = null;StringReader 阅读器 = null;//捕获全局异常,记录并抛出尝试{reader = new StringReader(path);输出 = (MethodCheckType)serializer.Deserialize(reader);}捕获(异常){扔;}最后{读者.处置();}返回输出.WebService;
<小时>
也许我可以用不同的话来说:我有这个 MethodCheckType
对象的一个实例,另一方面,我有我序列化这个对象的 XML 文档.现在我想将此实例转换为字符串形式的 XML 文档.在此之后,我必须证明两个字符串(XML 文档)是否相同.我必须这样做,因为我对第一种方法进行了单元测试,在该方法中我将 XML 文档读入 StringReader
并将其序列化为 MethodCheckType
对象.
这里是两种方式的转换方法.this = 你的类的实例
公共字符串 ToXML(){using(var stringwriter = new System.IO.StringWriter()){var serializer = new XmlSerializer(this.GetType());serializer.Serialize(stringwriter, this);返回 stringwriter.ToString();}}公共静态 YourClass LoadFromXMLString(string xmlText){使用(var stringReader = new System.IO.StringReader(xmlText)){var serializer = new XmlSerializer(typeof(YourClass));返回 serializer.Deserialize(stringReader) 作为 YourClass ;}}
I have got a class named WebserviceType
I got from the tool xsd.exe from an XSD file.
Now I want to deserialize an instance of an WebServiceType
object to a string.
How can I do this?
The MethodCheckType
object has as params a WebServiceType
array.
My first try was like I serialized it: with a XmlSerializer
and a StringWriter
(while serializing I used a StringReader
).
This is the method in which I serialize the WebServiceType
object:
XmlSerializer serializer = new XmlSerializer(typeof(MethodCheckType));
MethodCheckType output = null;
StringReader reader = null;
// catch global exception, logg it and throw it
try
{
reader = new StringReader(path);
output = (MethodCheckType)serializer.Deserialize(reader);
}
catch (Exception)
{
throw;
}
finally
{
reader.Dispose();
}
return output.WebService;
Edit:
Maybe I could say it in different words: I have got an instance of this MethodCheckType
object an on the other hand I have got the XML document from which I serialized this object. Now I want to convert this instance into a XML document in form of a string. After this I have to proof if both strings (of XML documents) are the same. This I have to do, because I make unit tests of the first method in which I read an XML document into a StringReader
and serialize it into a MethodCheckType
object.
Here are conversion method for both ways. this = instance of your class
public string ToXML()
{
using(var stringwriter = new System.IO.StringWriter())
{
var serializer = new XmlSerializer(this.GetType());
serializer.Serialize(stringwriter, this);
return stringwriter.ToString();
}
}
public static YourClass LoadFromXMLString(string xmlText)
{
using(var stringReader = new System.IO.StringReader(xmlText))
{
var serializer = new XmlSerializer(typeof(YourClass ));
return serializer.Deserialize(stringReader) as YourClass ;
}
}
这篇关于将对象转换为 XML 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!