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

时间:2013-11-05 20:42:43

标签: c# .net xml xml-serialization xml-deserialization

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

2 个答案:

答案 0 :(得分:0)

您可以尝试将<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>

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

答案 1 :(得分:0)

您的DataContract属性断言CustomerLeads元素应位于http://www.w3.org/2001/XMLSchema-instance XML命名空间中。它不是。在您的XML中,CustomerLeads没有命名空间(实际上是空命名空间)。

尝试从Namespace属性中删除DataContract设置。