从Object获取纯XML

时间:2011-02-17 18:56:00

标签: c# xml-serialization

我有以下代码将Object转换为XML并且工作正常。

    public static string ConvertObjectToXML(Object obj)
    {
        String XmlizedString = null;
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = null;

            if (obj is DerivedClass2)
            {
                xs = new XmlSerializer(typeof(DerivedClass2));
            }

        TextWriter w = new StringWriter();
        //this.s = new XmlSerializer(this.type);
        xs.Serialize(w, notoficationOrder);
        w.Flush();
        //return w;
        XmlizedString = w.ToString();
        w.Close();
        return XmlizedString.Trim();
   }

它提供以下输出

<?xml version="1.0" encoding="utf-16"?>*   
<Obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <List>
        <!--...-->
    </List>
</Obj>

但是我不想要描述Xml Namespace xd等的XML。我只需要纯粹的Object输出,如下所示

<Obj>
  <List>
     <!--...-->
  </List>
</Obj>

由于

海洋

1 个答案:

答案 0 :(得分:1)