Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This operation would create an incorrectly structured document

I am new to XML and tried the following but I'm getting an exception. Can someone help me?

The exception is This operation would create an incorrectly structured document

My code:

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);
}
like image 716
Vivekh Avatar asked Dec 29 '12 12:12

Vivekh


3 Answers

Xml document must have only one root element. But you are trying to add both Departments and Employees nodes at root level. Add some root node to fix this:

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"))))
                );
like image 117
Sergey Berezovskiy Avatar answered Nov 05 '22 07:11

Sergey Berezovskiy


You need to add root element.

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")))));
like image 12
Tilak Avatar answered Nov 05 '22 07:11

Tilak


In my case I was trying to add more than one XElement to xDocument which throw this exception. Please see below for my correct code which solved my issue

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

Please see below for what I get in distributorInfo

<Distributors>
 <Distributor Id="12345678" />
 <Distributor Id="22222222" />
</Distributors>
like image 2
Ziggler Avatar answered Nov 05 '22 07:11

Ziggler