定义新 xml 文件时使用两个命名空间(XDocument、XElement、XAttribute)

发布于 2024-08-30 05:55:49 字数 1429 浏览 11 评论 0原文

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

导致运行时错误:

{"The prefix '' cannot be redefined from '' to 'http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition' within the same start element tag."}

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

编辑:

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

改变并得到:

Additional information: The ':' character, hexadecimal value 0x3A, cannot be included in a name.

相反

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

Results in runtime error:

{"The prefix '' cannot be redefined from '' to 'http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition' within the same start element tag."}

What I am trying to do is just make the DataSources and DataSets write out to the Debug.Console to build ObjectDataSources since VS2010 neglected to add them for ASPX.

EDIT:

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

Changed and got :

Additional information: The ':' character, hexadecimal value 0x3A, cannot be included in a name.

Instead

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不美如何 2024-09-06 05:55:49

试试这个:

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());
    }
}

Try this:

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());
    }
}
久随 2024-09-06 05:55:49
            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(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"))
                        );

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文