解析器缺少XML命名空间
问题描述:
我不得不解析一个xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”命名空间丢失的XML,所以xml看起来像这样:解析器缺少XML命名空间
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<program>
<scriptList>
<script type="StartScript">
<isUserScript>false</isUserScript>
</script>
</scriptList>
</program>
,但应该是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<program xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<scriptList>
<script xsi:type="StartScript">
<isUserScript>false</isUserScript>
</script>
</scriptList>
</program>
类型属性确保正确的子类如
class StartScript : script
{...}
解析器是从手写的xsd通过$> xsd.exe a.xsd/classes(.Net)自动生成的。 这里是XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="qualified">
<!-- Main element -->
<xs:element name="program">
<xs:complexType>
<xs:sequence>
<xs:element name="scriptList">
<xs:complexType>
<xs:sequence>
<xs:element name="script" type="script" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="script" />
<xs:complexType name="StartScript">
<xs:complexContent>
<xs:extension base="script">
<xs:all>
<xs:element name="isUserScript" type="xs:boolean"></xs:element>
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>
一个简单的解决方案是运行一个字符串替换:对输入XML(“类型= \‘’到‘的xsi类型= \’”),但是这是非常难看。 有更好的解决方案吗?
答
您可以将XML加载到一个中间LINQ to XMLXDocument
,固定在<script>
元素属性的名称空间,然后直接反序列化到最终等级:
// Load to intermediate XDocument
XDocument xDoc;
using (var reader = XmlReader.Create(f))
xDoc = XDocument.Load(reader);
// Fix namespace of "type" attributes
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
foreach (var element in xDoc.Descendants("script"))
{
var attr = element.Attribute("type");
if (attr == null)
continue;
var newAttr = new XAttribute(xsi + attr.Name.LocalName, attr.Value);
attr.Remove();
element.Add(newAttr);
}
// Deserialize directly to final class.
var program = xDoc.Deserialize<program>();
使用扩展方法:
public static class XObjectExtensions
{
public static T Deserialize<T>(this XContainer element, XmlSerializer serializer = null)
{
if (element == null)
throw new ArgumentNullException();
using (var reader = element.CreateReader())
return (T)(serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
}
}
你如何解析它?你在使用'XmlSerializer'还是其他的东西?还有其他类型的'script'还是只有'StartScript'? – dbc
解析在C#中很简单: _program =(new XmlSerializer(typeof(program)))。反序列化(f)为程序; 有很多脚本,StartScript只是一个例子 – Harald