This operation would create an incorrectly structured document

后端 未结 3 451
忘掉有多难
忘掉有多难 2020-12-18 19:31

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 inc

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 19:50

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

提交回复
热议问题