【发布时间】:2016-04-06 12:43:06
【问题描述】:
class Customers
{
public int CustId { get; set; }
public string Name { get; set; }
public long MobileNo { get; set; }
public string Location { get; set; }
public string Address { get; set; }
StringWriter stringWriter = new StringWriter();
// UserInput
public void InsertCustomer()
{
Console.WriteLine("Enter Your Id");
CustId = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Your Name");
Name = Console.ReadLine();
Console.WriteLine("Enter Your Mobile No");
MobileNo = long.Parse(Console.ReadLine());
Console.WriteLine("Enter Your Location");
Location = Console.ReadLine();
Console.WriteLine("Enter Your Address");
Address = Console.ReadLine();
try
{
XmlDocument doc = new XmlDocument();
doc.Load(@"CustomersDetail.xml");
if (doc.ChildNodes.Count == 0)
{
// It is empty
XDocument xDoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("LINQ To XML Demo"),
new XElement("Customers",
new XElement("Customer",
new XElement("CustId", CustId),
new XElement("Name", Name),
new XElement("MobileNo", MobileNo),
new XElement("Location", Location),
new XElement("Address", Address))));
xDoc.Save(stringWriter);
xDoc.Save(@"CustomersDetail.xml");
Console.WriteLine("\n Created new XML \n" + stringWriter);
}
else if (doc.ChildNodes.Count > 1)
{
//if (xDoc.ChildNodes.Count > 1)
// There are more children on the **root level** of the DOM
XDocument xdoc = XDocument.Load(@"CustomersDetail.xml");
xdoc.Element("Customers").Add(
new XElement("Customer",
new XElement("CustId", CustId),
new XElement("Name", Name),
new XElement("MobileNo", MobileNo),
new XElement("Location", Location),
new XElement("Address", Address)));
xdoc.Save(stringWriter);
xdoc.Save(@"CustomersDetail.xml");
Console.WriteLine("\n Added \n" + stringWriter);
}
}
catch(XmlException exc)
{
//invalid file
Console.WriteLine("Sorry");
}
}
}
我正在尝试创建 XML Db
StringWriter stringWriter = new StringWriter();
XDocument xDoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("LINQ To XML Demo"),
new XElement("Customers",
new XElement("Customer",
new XElement("CustId", CustId),
new XElement("Name", Name),
new XElement("MobileNo", MobileNo),
new XElement("Location", Location),
new XElement("Address", Address))));
xDoc.Save(stringWriter);
xDoc.Save(@"CustomersDetail.xml");
Console.WriteLine("\n Created new XML \n" + stringWriter);
我运行此代码正常工作,但重新运行代码它会丢失我以前的数据和新数据。
然后是在现有 XML 上添加新数据的新代码
XDocument xdoc = XDocument.Load(@"CustomersDetail.xml");
xdoc.Element("Customers").Add(
new XElement("Customer",
new XElement("CustId", CustId),
new XElement("Name", Name),
new XElement("MobileNo", MobileNo),
new XElement("Location", Location),
new XElement("Address", Address)));
xdoc.Save(stringWriter);
xdoc.Save(@"CustomersDetail.xml");
Console.WriteLine("\n Added \n" + stringWriter);
但是运行这段代码
XDocument xdoc = XDocument.Load(@"CustomersDetail.xml");
System.Xml.dll 中出现“System.Xml.XmlException”类型的未处理异常
附加信息:缺少根元素。
帮帮我
【问题讨论】:
-
当您尝试运行该行时文件是什么样的?
-
文件是空的
-
那么看来错误信息是正确的。有空文件怎么办?
-
你的代码对我有用,虽然我把保存到
StringWriter。这有什么意义? -
@SentilSebastian - 您的异常可能是因为在之前的程序运行中,您创建了无效的 XML 文件。