简体   繁体   English

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

[英]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.

相关问题 避免在.Net DataContractSerializer中使用“http://www.w3.org/2001/XMLSchema-instance”命名空间 - Avoiding using the “http://www.w3.org/2001/XMLSchema-instance” namespace with .Net DataContractSerializer 使用 DataContractSerializer 时删除 xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; - remove xmlns:i=“http://www.w3.org/2001/XMLSchema-instance” when using DataContractSerializer 删除 p2:type="&lt;<type> &gt;" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" 来自 xml</type> - Remove p2:type="<<type>>" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" from xml 具有“ http://www.w3.org/2001/XMLSchema”名称空间的性能影响 - Performance impact of having “http://www.w3.org/2001/XMLSchema” namespace 元素http://www.w3.org/2001/XMLSchema:complexType在此上下文中无效 - Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context C#中的XML反序列化错误-InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> 没想到 - XML Deserialization Error in C# - InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> was not expected “'http://www.w3.org/XML/1998/namespace:lang' 属性未声明。” - “The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared.” JWT Header 算法:“hs256”与“http://www.w3.org/2001/04/xmldsig-more#hmac-sha256”相同 - JWT Header algorithm: is "hs256" the same as "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256" DNX Core 5.0 JwtSecurityTokenHandler“ IDX10640:不支持算法:&#39;http://www.w3.org/2001/04/xmldsig-more#hmac-sha256&#39;” - DNX Core 5.0 JwtSecurityTokenHandler “IDX10640: Algorithm is not supported: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256'” SignedXml CanonicalizationMethod - http://www.w3.org/2006/12/xml-c14n11 - SignedXml CanonicalizationMethod - http://www.w3.org/2006/12/xml-c14n11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM