0

我开发了以下代码来生成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.

有人可以帮忙吗?

4

1 回答 1

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" />
于 2012-09-26T17:50:02.017 回答