[英]Expecting element 'CustomerLeads' from namespace 'http://www.w3.org/2001/XMLSchema-instance'
Im getting the following error when running my application: Additional information: Error in line 2 position 64. Expecting element 'CustomerLeads' from namespace ' http://www.w3.org/2001/XMLSchema-instance '.. Encountered 'Element' with name 'CustomerLeads', namespace ''. 我在运行我的应用程序时遇到以下错误:其他信息:第2行第64位错误。期望来自名称空间' http://www.w3.org/2001/XMLSchema-instance '的元素'CustomerLeads'。.遇到'Element'名称为“ CustomerLeads”,名称空间为“”。
I dont understand why I am getting this error message because as you can see from the XML 'CustomerLeads' is included within the XML. 我不明白为什么会收到此错误消息,因为从XML中可以看到,XML中包含“ CustomerLeads”。 If I take out the namespace the file will not read the Elements. 如果我删除名称空间,该文件将不会读取元素。 Including the namespace seems to work but cant seem to figure out why I am getting this error. 包括名称空间似乎可行,但似乎无法弄清为什么我得到此错误。 And how I can add the namespace without getting this error? 以及如何添加名称空间而不出现此错误?
<?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>
Code: 码:
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;
}
}
}
you might try <xsi:CustomerLeads>
xsi being the xml namespace. 您可以尝试将<xsi:CustomerLeads>
xsi作为xml名称空间。 Seems like the error is saying it doesn't know what namespace CustomerLeads belongs to. 似乎该错误表明它不知道CustomerLeads属于哪个命名空间。
<xsi: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>
</xsi:CustomerLeads>
The prefix might be required for the other elements as well. 其他元素也可能需要前缀。
Your DataContract
attribute asserts that the CustomerLeads
element is expected to be in the http://www.w3.org/2001/XMLSchema-instance
XML namespace. 您的DataContract
属性断言, CustomerLeads
元素应位于http://www.w3.org/2001/XMLSchema-instance
XML名称空间中。 It is not. 它不是。 In your XML, CustomerLeads
has no namespace (effectively an empty namespace). 在您的XML中, CustomerLeads
没有命名空间(实际上是一个空的命名空间)。
Try removing the Namespace
setting from your DataContract
attribute. 尝试从DataContract
属性中删除“ Namespace
设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.