C# 使用XAttribute添加和验证命名空间属性值
我想使用XObject以编程方式创建一个“dcterms:type”元素,该元素具有名称空间属性和属性值。目标xml如下所示:C# 使用XAttribute添加和验证命名空间属性值,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我想使用XObject以编程方式创建一个“dcterms:type”元素,该元素具有名称空间属性和属性值。目标xml如下所示: <metadata xmlns="http://example.org/myapp/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.org/myapp/ http://example.
<metadata
xmlns="http://example.org/myapp/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/myapp/ http://example.org/myapp/schema.xsd"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/">
...
<dcterms:type xsi:type="dcterms:URI">test</dcterms:type>
...
</metadata>
但这会产生:
<dc:type xsi:type="{http://purl.org/dc/terms/}Text">test</dc:type>
这将生成正确的xml,但通过XDocument.Validate()验证时返回“这是无效的xsi:type”“”
想法?您确定在xml中也添加了正确的xsd标记吗?请参阅MSDN验证工作原理:是的,我的名称空间没有正确分类。谢谢你看!
<dc:type xsi:type="{http://purl.org/dc/terms/}Text">test</dc:type>
XAttribute typeAttribute = new XAttribute(xsi + "type", @"dcterms:URI");