【问题标题】:Using two namespaces when defining a new xml file (XDocument, XElement, XAttribute)定义新的 xml 文件时使用两个命名空间(XDocument、XElement、XAttribute)
【发布时间】:2010-04-22 15:48:00
【问题描述】:
XNamespace xnRD = "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner";
XNamespace xnNS = "http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition";

XAttribute xaRD = new XAttribute(XNamespace.Xmlns + "rd", xnRD);
XAttribute xaNS = new XAttribute("xmlns", xnNS);

XElement x =
                new XElement("Report", xaRD, xaNS,
                    new XElement("DataSources"),
                    new XElement("DataSets"),
                    new XElement("Body"),
                    new XElement("Width"),
                    new XElement("Page"),
                    new XElement("ReportID", xaRD),
                    new XElement("ReportUnitType", xaRD)
                );

XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
doc.Add(x);
Console.WriteLine(doc.ToString());

导致运行时错误:

{"前缀 '' 不能在同一个起始元素标记内从 '' 重新定义为 'http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition'。"}

我想做的只是让 DataSources 和 DataSets 写出到 Debug.Console 以构建 ObjectDataSources,因为 VS2010 忽略了为 ASPX 添加它们。

编辑:

                    new XElement(xaRD + "ReportID"),
                    new XElement(xaRD + "ReportUnitType")

改变并得到:

附加信息:“:”字符,十六进制值 0x3A,不能包含在名称中。

相反

【问题讨论】:

    标签: xml reporting c#-4.0 rdlc


    【解决方案1】:

    试试这个:

    using System;
    using System.Xml.Linq;
    
    class Example
    {
        static void Main()
        {
            XNamespace xnRD = "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner";
            XNamespace xnNS = "http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition";
    
            XDocument doc = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                        new XElement(xnNS + "Report",
                        new XAttribute(XNamespace.Xmlns + "rd", xnRD),
                            new XElement("DataSources"),
                            new XElement("DataSets"),
                            new XElement("Body"),
                            new XElement("Width"),
                            new XElement("Page"),
                            new XElement(xnRD + "ReportID"),
                            new XElement(xnRD + "ReportUnitType")));
    
            Console.WriteLine(doc.ToString());
        }
    }
    

    【讨论】:

    【解决方案2】:

                XNamespace xnRD = "@987654321@";
                XNamespace xnNS = "@987654322@";
    
    
            XDocument doc = new XDocument(
                    new XDeclaration
                        (
                            "1.0",
                            "utf-8",
                            "yes"
                        ),
                        new XElement
                            (
                                xnNS + "Report",
                                new XAttribute(XNamespace.Xmlns + "rd", xnRD),
                                new XElement(xnNS + "DataSources"),
                                new XElement(xnNS + "DataSets"),
                                new XElement(xnNS + "Body"),
                                new XElement(xnNS + "Width"),
                                new XElement(xnNS + "Page"),
                                new XElement(xnRD + "ReportID"),
                                new XElement(xnRD + "ReportUnitType"))
                            );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多