2

我目前有:

XNamespace xmlns = "XSDName";<br>
XNamespace xsi = @"http://www.w3.org/2001/XMLSchema-instance";<br>
XNamespace schemaloc = @"XSDName XSDName.xsd";
XDocument xdoc = new XDocument(
    new XElement("BaseReport",
    new XAttribute(xsi + "schemaLocation", schemaloc),
    new XAttribute(XNamespace.Xmlns+"ns1", xmlns),
    new XAttribute(XNamespace.Xmlns + "xsi", xsi));

这给了我:

BaseReport xsi:schemaLocation="XSDName XSDName .xsd" xmlns:ns1="XSDName" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

我怎么能BaseReport读懂ns1:BaseReport

4

1 回答 1

4

下面的代码将为您提供所需的输出。关键是在名称之前添加已定义的命名空间并让 .NET 找出正确的前缀。

XNamespace xmlns = "XSDName";
XNamespace xsi = @"http://www.w3.org/2001/XMLSchema-instance";
XNamespace schemaloc = @"XSDName XSDName.xsd";
XDocument xdoc = new XDocument(
    new XElement(xmlns + "BaseReport",
    new XAttribute(xsi + "schemaLocation", schemaloc),
    new XAttribute(XNamespace.Xmlns + "ns1", xmlns),
    new XAttribute(XNamespace.Xmlns + "xsi", xsi)));
于 2013-02-20T22:01:31.413 回答