I want to generate this 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">
The problem is the namespace in xsi:schemaLocation
attribute.
here is my code:
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()));
It generates this header and I don't know why.
<?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">
I found the following code online:
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")
)
);
I haven't really used the new XML features of C#, but it looks like you need to declare the xsi
namespace properly.
See: http://www.somethingorothersoft.com/2010/01/28/adding-schemalocation-attribute-to-xelement-in-linq-to-sql/ for more info.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.