簡體   English   中英

將xmlns屬性添加到根元素

[英]Add xmlns attribute to root element

我有C#程序來為Reporting Services中的鞭子秀報告生成RDL文件。 我使用Linq將Xml生成Xml。

當我嘗試將xmlns XAttribute添加到報表元素時,遇到了幾個問題。

我測試以下方法:

第一:

        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">

嘗試按照如何:使用命名空間(C#)(LINQ to XML)創建文檔中的說明使用XNamespace添加子節點。

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"));

您可以從@Filburt的答案以及本文中看到,xmlns屬性是一個特殊屬性。 只能通過XNamespace類訪問它。

下面,我將為您提供有關如何創建名稱空間的示例。 您應該查看如何:使用命名空間創建文檔以獲取更多信息。 您的代碼向所有子級添加xmlns標記的原因是因為您沒有在同一名稱空間中創建所有子級節點。

  1. 要將元素放置在默認名稱空間中,請創建一個XNamespace (請參見下面的ns1),然后將該值放在元素名稱的前面。 例如: new XElement(ns1 + "Report"); 這將在ns1命名空間中創建元素<Report> ,並且沒有前綴。
  2. 要添加其他名稱空間,請添加帶有名稱空間和前綴的屬性。 例如, new XAttribute(XNamespace.Xmlns + "ns2", ns2)將一個命名空間添加到<Report>元素中,並帶有ns2前綴。 此后,每次使用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()); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM