使用xsi:schemaLocation命名空间创建XDocument [英] Creating XDocument with xsi:schemaLocation namespace
本文介绍了使用xsi:schemaLocation命名空间创建XDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建以下XML,并且我正在尝试使用XDocument进行此操作.但是,我在指定名称空间时遇到了麻烦.
I need to create the following XML and I'm trying to do this using XDocument. However, I'm having trouble specifying the name spaces.
<AssessmentOrderRequest
xsi:schemaLocation="http://ns.hr-xml.org/2007-04-15 http://ns.hr-xml.org/2_5/HR-XML-2_5/StandAlone/AssessmentOrderRequest.xsd"
xmlns="http://ns.hr-xml.org/2007-04-15"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</AssessmentOrderRequest>
这是我要寻找的那种代码,但是,我无法在xsi:schemaLocation
名称中创建带有冒号的属性.
This is the sort of code that I'm looking for, however, I can't create attributes with a colon in the name for the xsi:schemaLocation
.
return new XDocument(
new XElement("AssessmentOrderRequest",
new XAttribute("xsi:schemaLocation", XNamespace.Get("http://ns.hr-xml.org/2007-04-15 http://ns.hr-xml.org/2_5/HR-XML-2_5/StandAlone/AssessmentOrderRequest.xsd")),
new XAttribute("xmlns", XNamespace.Get("http://ns.hr-xml.org/2007-04-15")),
new XAttribute(XNamespace.Xmlns + "xsi", XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"))
)
);
推荐答案
这是因为xsi
本身就是名称空间.您将需要执行以下操作:
This is because the xsi
is a namespace in itself. You would need to do something like:
XNamespace xmlns = XNamespace.Get("http://ns.hr-xml.org/2007-04-15");
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace schemaLocation = XNamespace.Get("http://ns.hr-xml.org/2007-04-15 http://ns.hr-xml.org/2_5/HR-XML-2_5/StandAlone/AssessmentOrderRequest.xsd");
return new XDocument(
new XElement(xmlns + "AssessmentOrderRequest",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", schemaLocation)
)
);
更新了我用来解决问题的最终代码.感谢James的原始回答.
Updated with final code that I used to solve the problem. With thanks to the original answer from James.
这篇关于使用xsi:schemaLocation命名空间创建XDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文