0
我想将类型T的对象转换为xml节点,因为我想在wpf控件中显示xml节点。下面是linqpad片段,我一起工作:如何从类型T的对象获取xml节点?
[Serializable]
public class test {
public int int1 { get ; set ;}
public int int2 { get ; set ;}
public string str1 { get ; set ;}
}
void Main()
{
test t1 = new test() ;
t1.int1 = 12 ;
t1.int2 = 23 ;
t1.str1 = "hello" ;
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(t1.GetType());
StringWriter sww = new StringWriter();
XmlWriter writer = XmlWriter.Create(sww);
x.Serialize(writer, t1);
var xml = sww.XmlSerializeToXElement();
xml.Dump() ;
}
我没有得到预期的效果,相反,我得到这个:
<StringWriter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NewLine>
</NewLine>
</StringWriter>