这是我将数据添加到xml的代码:
IsolatedStorageFile isstore = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream bookfile = new IsolatedStorageFileStream("People.xml", System.IO.FileMode.Open, isstore);
XDocument xmldetails = XDocument.Load(bookfile);
XElement books =
new XElement("person",
new XAttribute("id", "5"),
new XAttribute("name", "Book Title"),
new XAttribute("beneficiary", "Book Author"),
new XAttribute("description", "Book Author"),
new XAttribute("deadline", "Book Author"),
new XAttribute("price", "Fiction"));
xmldetails.Root.Add(books);
xmldetails.Save(bookfile);
bookfile.Close();
这是People.xml:
<?xml version="1.0" encoding="utf-8" ?>
<people>
<person index="1" name="Zlecenie numer jeden" beneficiary="Kowalski" description="Proste zlecenie jakiejs strony czy cos" price="800" deadline="27.12.2013" />
</people>
当我点击按钮时出现此错误:
根级别的数据无效。第1行,第1位。
答案 0 :(得分:1)
您的XML文件可能缺少根节点,并且您尝试将子节点添加到不存在的父节点。确保您的源XML格式正确。
顺便说一下,在代码中你应该做的是:
XElement books =
new XElement("person",
new XAttribute("id", "5"),
new XAttribute("name", "Book Title"),
new XAttribute("beneficiary", "Book Author"),
new XAttribute("description", "Book Author"),
new XAttribute("deadline", "Book Author"),
new XAttribute("price", "Fiction"));
xmldetails.Element("People").Add(books);
答案 1 :(得分:1)