【问题标题】:This operation would create an incorrectly structured document此操作会创建结构不正确的文档
【发布时间】:2012-12-29 12:48:09
【问题描述】:

我是 XML 的新手,并尝试了以下操作,但遇到了异常。有人能帮我吗?

例外是This operation would create an incorrectly structured document

我的代码:

string strPath = Server.MapPath("sample.xml");
XDocument doc;
if (!System.IO.File.Exists(strPath))
{
    doc = new XDocument(
        new XElement("Employees",
            new XElement("Employee",
                new XAttribute("id", 1),
                    new XElement("EmpName", "XYZ"))),
        new XElement("Departments",
            new XElement("Department",
                new XAttribute("id", 1),
                    new XElement("DeptName", "CS"))));

    doc.Save(strPath);
}

【问题讨论】:

    标签: c# asp.net .net linq linq-to-xml


    【解决方案1】:

    Xml 文档必须只有一个根元素。但是您正在尝试在根级别添加 Departments 和 Employees 节点。添加一些根节点来解决这个问题:

    doc = new XDocument(
        new XElement("RootName",
            new XElement("Employees",
                new XElement("Employee",
                    new XAttribute("id", 1),
                    new XElement("EmpName", "XYZ"))),
    
            new XElement("Departments",
                    new XElement("Department",
                        new XAttribute("id", 1),
                        new XElement("DeptName", "CS"))))
                    );
    

    【讨论】:

    • 他们可能会考虑让这个错误信息更加明确。诸如“XML 文档可能只有一个根元素”之类的东西。即使知道这一事实,仅凭此错误消息也很难理解问题。
    【解决方案2】:

    你需要添加根元素。

    doc = new XDocument(new XElement("Document"));
        doc.Root.Add(
            new XElement("Employees",
                new XElement("Employee",
                    new XAttribute("id", 1),
                        new XElement("EmpName", "XYZ")),
            new XElement("Departments",
                new XElement("Department",
                    new XAttribute("id", 1),
                        new XElement("DeptName", "CS")))));
    

    【讨论】:

      【解决方案3】:

      在我的情况下,我试图向 xDocument 添加多个 XElement,这会引发此异常。请参阅下面的正确代码来解决我的问题

      string distributorInfo = string.Empty;
      
              XDocument distributors = new XDocument();
              XElement rootElement = new XElement("Distributors");
              XElement distributor = null;
              XAttribute id = null;
      
      
              distributor = new XElement("Distributor");
              id = new XAttribute("Id", "12345678");
              distributor.Add(id);
              rootElement.Add(distributor);
      
              distributor = new XElement("Distributor");
              id = new XAttribute("Id", "22222222");
              distributor.Add(id);
              rootElement.Add(distributor);
      
              distributors.Add(rootElement);
      
      
       distributorInfo = distributors.ToString();
      

      请看下面我在distributorInfo中得到的信息

      <Distributors>
       <Distributor Id="12345678" />
       <Distributor Id="22222222" />
      </Distributors>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多