Add xmlns attribute to root element

I have a C # program to generate an RDL file. To show a report in Reporting Services. I am using Linq for Xml to create Xml.

When I try to add XAttribute to the xmlns report element , I encounter several problems.

I am testing the following methods:

the first:

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

this is part of my code showing how to generate xml:

second:

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

The first method returns an error, and the second method adds the xmlns attribute to all child nodes.

I want this format:

<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition">
+5
source share
2 answers

XNamespace, (#) (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"));
+4

@Filburt this, xmlns . XNamespace.

, . . , xmlns , , .

  • , XNamespace (. ns1) . : new XElement(ns1 + "Report"); <Report> ns1 .
  • , . , new XAttribute(XNamespace.Xmlns + "ns2", ns2) <Report> ns2. , (new XElement(ns2+"DataSources")) ns2, . . .

        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());
    
+2

All Articles