使用 xsi:schemaLocation 命名空间创建 XDocument
我需要创建以下 XML,并尝试使用 XDocument 来完成此操作。但是,我在指定名称空间时遇到问题。
<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
名称中带有冒号的属性。
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"))
)
);
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>
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"))
)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
xsi
本身就是一个命名空间。您需要执行以下操作:编辑:用我用来解决问题的最终代码进行更新。感谢詹姆斯的原始回答。
This is because the
xsi
is a namespace in itself. You would need to do something like:EDIT: Updated with final code that I used to solve the problem. With thanks to the original answer from James.