我有像下面这样的WCF对象
[XmlRoot("Result")]
public class Result0:IXmlSerializable
{
public XmlSchema GetSchema() { return null; }
public void ReadXml(XmlReader reader) { xxx; }
public void WriteXml(XmlWriter writer) { xxx; }
}
[XmlRoot("Result")]
public class Result1:IXmlSerializable
{
public XmlSchema GetSchema() { return null; }
public void ReadXml(XmlReader reader) { xxx; }
public void WriteXml(XmlWriter writer) { xxx; }
}
当我构建此WCF应用程序时,我将得到以下错误
InvalidOperationException: The top XML element 'Result' from namespace '' references distinct types
我知道我可以在XmlRoot中添加选项NameSpace param来解决这个冲突。但我想要的是获取没有任何xmlns的Xml
<Result>
<xxxx></xxxx>
</Result>
有解决方法吗?
答案 0 :(得分:0)
public static XmlDocument RemoveXmlns(XmlDocument doc)
{
XDocument d;
using (var nodeReader = new XmlNodeReader(doc))
d = XDocument.Load(nodeReader);
d.Root.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove();
foreach (var elem in d.Descendants())
elem.Name = elem.Name.LocalName;
var xmlDocument = new XmlDocument();
using (var xmlReader = d.CreateReader())
xmlDocument.Load(xmlReader);
return xmlDocument;
}
public static XmlDocument RemoveXmlns(String xml)
{
XDocument d = XDocument.Parse(xml);
d.Root.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove();
foreach (var elem in d.Descendants())
elem.Name = elem.Name.LocalName;
var xmlDocument = new XmlDocument();
xmlDocument.Load(d.CreateReader());
return xmlDocument;
}