这是我的代码,它不断抛出 System.Xml.XmlException: Element 'Customer' was not found.And XML file pasteed at bottom
public static List<Customer> GetCustomers()
{
// create the list
List<Customer> customers = new List<Customer>();
// create the XmlReaderSettings object
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
// create the XmlReader object
XmlReader xmlIn = XmlReader.Create(path, settings);
// read past all nodes to the first Customer node
xmlIn.ReadToDescendant("Customers");
// create one Customer object for each Customer node
do
{
Customer c = new Customer();
xmlIn.ReadStartElement("Customer");
c.FirstName =
xmlIn.ReadElementContentAsString();
c.LastName =
xmlIn.ReadElementContentAsString();
c.Email =
xmlIn.ReadElementContentAsString();
customers.Add(c);
}
while (xmlIn.ReadToNextSibling("Customer"));
// close the XmlReader object
xmlIn.Close();
return customers;
这是我的 XML,它清楚地包含 Element Customer
<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<Email>jsmith@gmail.com</Email>
</Customer>
<Customer>
<FirstName>Jane</FirstName>
<LastName>Doe</LastName>
<Email>janedoe@yahoo.com</Email>
</Customer>
</Customers>