我开发了以下代码来生成xElement
(C#语言):
new XElement("Transmission",
new XAttribute(XNamespace.Xmlns.GetName("xmlns").LocalName, "http://www.irs.gov/efile"),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", schemaLocation),
........
这三个名称空间的声明如下:
private static XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
private static XNamespace schemaLocation = XNamespace.Get("http://www.irs.gov/efile ./ExtendedCommon/Transmission.xsd");
private static XNamespace xmlnsLocation = XNamespace.Get("http://www.irs.gov/efile");
但是当我从中生成xml时,我收到以下错误:
The prefix '' cannot be redefined from '' to 'http://www.irs.gov/efile' within the same start element tag.
有人可以帮忙吗?
答案 0 :(得分:0)
我想你想要
XNamespace df = "http://www.irs.gov/efile";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
string schemaLocation = "ExtendedCommon/Transmission.xsd";
XElement t = new XElement(df + "Transmission",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", string.Format("{0} {1}", df, schemaLocation)));
这将生成XML
<Transmission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.irs.gov/efile ExtendedCommon/Transmission.xsd" xmlns="http://www.irs.gov/efile" />