2
votes

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 ''.

I dont understand why I am getting this error message because as you can see from the XML 'CustomerLeads' is included within the XML. 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>[email protected]</Email>
  </CustomerLead>-<CustomerLead>
    <FirstName>Nicole</FirstName>
    <LastName>Farhi</LastName>
    <Email>[email protected]</Email>
  </CustomerLead>-<CustomerLead>
    <FirstName>Raf</FirstName>
    <LastName>Simons</LastName>
    <Email>[email protected]</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;
        }
    }
}
2

2 Answers

0
votes

you might try <xsi:CustomerLeads> xsi being the xml namespace. Seems like the error is saying it doesn't know what namespace CustomerLeads belongs to.

<xsi:CustomerLeads xsi:noNamespaceSchemaLocation="BasicCustomerLead.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CustomerLead>
    <FirstName>Paul</FirstName>
    <LastName>Smith</LastName>
    <Email>[email protected]</Email>
  </CustomerLead>-<CustomerLead>
    <FirstName>Nicole</FirstName>
    <LastName>Farhi</LastName>
    <Email>[email protected]</Email>
  </CustomerLead>-<CustomerLead>
    <FirstName>Raf</FirstName>
    <LastName>Simons</LastName>
    <Email>[email protected]</Email>
  </CustomerLead>
</xsi:CustomerLeads>

The prefix might be required for the other elements as well.

0
votes

Your DataContract attribute asserts that the CustomerLeads element is expected to be in the http://www.w3.org/2001/XMLSchema-instance XML namespace. It is not. In your XML, CustomerLeads has no namespace (effectively an empty namespace).

Try removing the Namespace setting from your DataContract attribute.