在Xml序列化C#中用空标签替换xsi:nil =“true”

时间:2015-01-12 10:47:18

标签: xml asp.net-mvc parsing object serialization

我正在尝试将mvc对象序列化为xml字符串

我正在使用此函数解析对象

public string ToXML(object oObject)
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlSerializer xmlSerializer = new XmlSerializer(oObject.GetType());
        using (MemoryStream xmlStream = new MemoryStream())
        {
            xmlSerializer.Serialize(xmlStream, oObject);
            xmlStream.Position = 0;
            xmlDoc.Load(xmlStream);
            return xmlDoc.InnerXml;
        }
    }

不幸的是,我希望我的xml架构对于可以为空的值,例如

而不是<fProjectGID xsi:nil="true" />

就像

<fProjectGID><fProjectGID/>

如何添加第二个标记并从我生成的xml中为每个可空值删除这个愚蠢的xsi:nill =“true”属性?

1 个答案:

答案 0 :(得分:0)

XmlDocument doc = new XmlDocument();
doc.Load(file);                           
XmlNode node = doc["Root"]["fProjectGID"]; \\ Go to your desired Node
node.RemoveAll(); \\It Will remove the  xsi:nil="true"
node.InnerText = " Put Here your desired text";  \\ Any Inner Text You want.