2012-05-12 169 views
5

我有C#程序来生成RDL文件在报表服务中显示报表。 我使用Linq to Xml来生成Xml。将xmlns属性添加到根元素

当我尝试添加xmlns XAttribute到Report Element时,我遇到了几个问题。

我测试下面的方法:

第一:

 XDocument d = new XDocument(
      new XDeclaration("1.0", "utf-8", "yes"), 
      new XElement("Report", 
       new XAttribute(XNamespace.Xmlns + "rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"), 
       new XAttribute(XNamespace.Xmlns + "cl", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition"), 
       new XAttribute("xmlns", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"), 
        new XElement("DataSources", ""), 
        new XElement("DataSets", ""), 
        new XElement("ReportSections", 

这是我的代码巫演示了如何生成XML部分:

第二:

XNamespace reportDef = "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"; 
     XDocument d = new XDocument(
      new XDeclaration("1.0", "utf-8", "yes"), 
      new XElement(reportDef + "Report", 
       new XAttribute(XNamespace.Xmlns + "rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"), 
       new XAttribute(XNamespace.Xmlns + "cl", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition"), 
        new XElement("DataSources", ""), 
        new XElement("DataSets", ""), 
        new XElement("ReportSections",... 

第一种方法返回错误,第二种方法将属性xmlns添加到所有子节点。

我想这种格式:

<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"> 

回答

4

尝试添加使用XNamespace子节点作为How to: Create a Document with Namespaces (C#) (LINQ to XML)

XNamespace reportDef = "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"; 
XElement root = new XElement(reportDef + "Report", 
    new XElement(reportDef + "Child", "child content")); 

这说明应该给你想要的结果。

你也可以通过添加xmlns属性

XElement xe = new XElement(reportDef + "Report", 
    new XAttribute("xmlns", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"), 
    new XElement(reportDef + "Child", "child content")); 
+1

我用这个解决办法,但不幸的是因为在此之前属性添加到所有子节点。 –

+0

似乎很奇怪,因为我无法重现这一点。尝试添加默认的命名空间属性,如我的第二个例子。也许更新你的问题,并显示更多的实际代码。 – Filburt

+0

不幸的是,它像以前 –

2

您可以从@ Filburt答案,并this后看到添加默认命名空间,即xmlns属性是一个特殊的属性。它只能通过XNamespace类访问。

下面我会举例说明如何创建名称空间。您应该查看How to: Create a Document with Namespaces以获取更多信息。代码添加的原因xmlns标签给所有的孩子是因为你没有在同一个命名空间中创建所有的孩子节点。

  1. 为了把元素的默认命名空间,创建一个XNamespace(见下文NS1)和预先设置的值的元素名称。例如:new XElement(ns1 + "Report");这会在ns1命名空间中创建一个元素<Report>,并且没有前缀。
  2. 要添加其他名称空间,请添加一个具有名称空间和前缀的属性。例如,new XAttribute(XNamespace.Xmlns + "ns2", ns2)使用ns2前缀将一个名称空间添加到<Report>元素。在此之后,每次使用ns2命名空间创建元素(new XElement(ns2+"DataSources"))时,都会使用前缀。前缀可以用在具有前缀声明的元素下的所有后代中。这是你犯了一个错误的地方。

    StringBuilder sb = new StringBuilder(); 
        XmlWriterSettings xws = new XmlWriterSettings(); 
        xws.OmitXmlDeclaration = true; 
        xws.Indent = true; 
    
        using (XmlWriter xw = XmlWriter.Create(sb, xws)) 
        { 
         XNamespace ns1 = "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"; 
         XNamespace ns2 = "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"; 
         XNamespace ns3 = "http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition"; 
         XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); 
         XElement reportElement = new XElement(ns1 + "Report", 
          new XAttribute(XNamespace.Xmlns + "ns2", ns2), 
          new XAttribute(XNamespace.Xmlns + "ns3", ns3)); 
         doc.Add(reportElement); 
    
         reportElement.Add(new XElement(ns2+"DataSources")); 
         reportElement.Add(new XElement(ns3+"DataSets")); 
         reportElement.Add(new XElement(ns1+"ReportSections")); 
    
         doc.WriteTo(xw); 
        } 
    
        System.Diagnostics.Debug.Write(sb.ToString()); 
    
+0

是的所有子节点都创建相同的方式。我编辑第一个和第二个示例代码。请检查一下。谢谢 –

+0

@AmirReza我已经添加了一个例子,并指出你的错误。基本上,你需要把'new XElement(reportDef +“DataSources”,“”)''而不是'new XElement(“DataSources”,“”)''。名字空间在儿童中缺失,所以他们最终会进入空的名字空间。 – user845279