gpt4 book ai didi

c# - 在属性中定义 schemaLocation

转载 作者:数据小太阳 更新时间:2023-10-29 02:22:50 26 4
gpt4 key购买 nike

我想生成这个 xml header :

<?xml version="1.0" encoding="UTF-8"?>
<ru:myobject XML_DATA="2.1.0"
xmlns:ru="http://www.aaa.com" xmlns:t="http://www.bbb.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.aaa.com aaa.xsd
http://www.bbb.com bbb.xsd
http://www.ccc.com ddd.xsd">

问题是 xsi:schemaLocation 属性中的 namespace 。

这是我的代码:

XNamespace mynamespace = "http://www.aaa.com/test/";
XNamespace xsi = "http://www.aaa.com/test/ aaa.xsd http://www.bbb.com bbb.xsd http://www.ccc.com ccc.xsd";
XElement myobject = new XElement(mynamespace + "myobject", new XAttribute("XML_DATA", "2.1.0"));
myobject.Add(new XAttribute(XNamespace.Xmlns + "ru", "http://www.aaa.com"));
myobject.Add(new XAttribute(XNamespace.Xmlns + "t", "http://www.bbb.com"));
myobject.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));

myobject.Add(new XAttribute(xsi+"schemaLocation", xsi.ToString()));

它生成这个标题,我不知道为什么。

<?xml version="1.0" encoding="UTF-8"?>
<ru:myobject XML_DATA="2.1.0"
xmlns:ru="http://www.aaa.com" xmlns:t="http://www.bbb.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
p3:schemaLocation=" http://www.aaa.com aaa.xsd http://www.bbb.com bbb.xsd http://www.ccc.com ddd.xsd"
xmlns:p3="http://www.aaa.com aaa.xsd http://www.bbb.com bbb.xsd http://www.ccc.com ddd.xsd">

最佳答案

我在网上找到了如下代码:

XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace defaultNamespace = XNamespace.Get("http://www.foo.bar");

XElement doc = new XElement(
new XElement(defaultNamespace + "root",
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XAttribute(xsi + "schemaLocation",
"http://www.foo.bar someSchema.xsd")
)
);

我还没有真正使用过 C# 的新 XML 功能,但看起来您需要正确声明 xsi 命名空间。

参见:http://www.somethingorothersoft.com/2010/01/28/adding-schemalocation-attribute-to-xelement-in-linq-to-sql/了解更多信息。

关于c# - 在属性中定义 schemaLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5422736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com