运行我的应用程序时出现以下错误:附加信息:第 2 行位置 64 中的错误。从命名空间“ http://www.w3.org/2001/XMLSchema-instance ”中期待元素“CustomerLeads ”..遇到“元素”名称为“CustomerLeads”,命名空间为“”。
我不明白为什么会收到此错误消息,因为您可以从 XML 中看到“CustomerLeads”包含在 XML 中。如果我取出命名空间,文件将不会读取元素。包括命名空间似乎有效,但似乎无法弄清楚为什么我会收到此错误。以及如何添加命名空间而不会出现此错误?
<?xml version="1.0" encoding="UTF-8"?>
<CustomerLeads xsi:noNamespaceSchemaLocation="BasicCustomerLead.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CustomerLead>
<FirstName>Paul</FirstName>
<LastName>Smith</LastName>
<Email>psmith@example.com</Email>
</CustomerLead>-<CustomerLead>
<FirstName>Nicole</FirstName>
<LastName>Farhi</LastName>
<Email>nicole.farhi@example.com</Email>
</CustomerLead>-<CustomerLead>
<FirstName>Raf</FirstName>
<LastName>Simons</LastName>
<Email>rafs@example.org</Email>
</CustomerLead>
</CustomerLeads>
代码:
namespace Customer
{
[DataContract(Name = "CustomerLeads", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public class CustomerLeads
{
[DataMember()]
public string FirstName { get; set; }
[DataMember()]
public string LastName { get; set; }
[DataMember()]
public string EmailAddress { get; set; }
public CustomerLeads unSortedLeads(string xmFilelPath)
{
// doc.Load("C:/Users/Admin/Downloads/potentialcustomers.xml");
ICollection<CustomerLeads> deserializedPerson;
CustomerLeads lead;
FileStream fs = new FileStream(xmFilelPath, FileMode.Open);
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
DataContractSerializer ser = new DataContractSerializer(typeof(CustomerLeads));
lead = (CustomerLeads)ser.ReadObject(reader, true);
reader.Close();
fs.Close();
FirstName = lead.FirstName.ToString();
// foreach(CustomerLeads leads in deserializedPerson.)
return lead;
}
}
}