gpt4 book ai didi

c# - 期望来自命名空间 'CustomerLeads' 的元素 'http://www.w3.org/2001/XMLSchema-instance'

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:06 24 4
gpt4 key购买 nike

我在运行我的应用程序时遇到以下错误:附加信息:第 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;
}
}
}

最佳答案

你可以试试 <xsi:CustomerLeads> xsi 是 xml 命名空间。似乎错误是说它不知道 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>

其他元素也可能需要前缀。

关于c# - 期望来自命名空间 'CustomerLeads' 的元素 'http://www.w3.org/2001/XMLSchema-instance',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19798565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com