反序列化XML,其中元素和属性具有重复的名称

时间:2015-07-07 10:24:24

标签: c# xml serialization

我想反序列化以下XML。问题是重复的元素名称和属性名称。我试过这个apporach Deserializing duplicate XML elements with unique attributes

但我一直在说:“帐号xmlns =''不是预期的。”

<?xml version="1.0" encoding="utf-8"?>
<Account>
  <Field APIName="WrntyID">1234</Field>
  <Field APIName="ExternalID">4321</Field>
  <Field APIName="CreationDate">07/04/2015 18:28:45</Field>
  <Field APIName="Name"></Field>
  <Field APIName="Phone"></Field>
  <Field APIName="Mobile"></Field>
  <Field APIName="Fax"></Field>
  <Field APIName="Email"></Field>
  <Field APIName="GovID"></Field>
  <Field APIName="Note"></Field>
  <Field APIName="IsActive">True</Field>
  <Field APIName="ConnectedToSuperBiz">False</Field>
  <Field APIName="Street"></Field>
  <Field APIName="City"></Field>
  <Field APIName="State"></Field>
  <Field APIName="Country">USA</Field>
  <Field APIName="ZipCode">33172</Field>
  <Field APIName="Discount">0</Field>
  <Field APIName="PriceLevelName">USD</Field>
  <Field APIName="Prop1"></Field>
  <Field APIName="Prop2"></Field>
  <Field APIName="Prop3"></Field>
  <Field APIName="Prop4"></Field>
  <Field APIName="Prop5"></Field>
  <Agents>
    <Agent>
      <Field APIName="WrntyID">54321</Field>
      <Field APIName="ExternalID"></Field>
    </Agent>
  </Agents>
  <Catalogs>
    <Catalog>
      <Field APIName="WrntyID">12345</Field>
      <Field APIName="ExternalID"></Field>
    </Catalog>
  </Catalogs>
  <Locations>
    <Location>
      <Field APIName="WrntyID">123456</Field>
      <Field APIName="ExternalID">0002</Field>
    </Location>
  </Locations>
  <Contacts>
    <Contact>
      <Field APIName="WrntyID">1234</Field>
      <Field APIName="ExternalID"></Field>
    </Contact>
  </Contacts>
</Account>

我的C#类现在看起来像这样。:

[XmlRoot]
    public class Account {        
        public Agents Agents { get; set; }
        public Catalogs Catalogs { get; set; }
        public Locations Locations { get; set; }
        public Contacts Contacts { get; set; }
    }
    public class Agents {
        [XmlArray("Agent")]
        [XmlArrayItem("Field", typeof(Values))]
        public Values[] Field { get; set; }
    }
    public class Catalogs {
        [XmlArray("Catalog")]
        [XmlArrayItem("Field", typeof(Values))]
        public Values[] Field { get; set; }
    }
    public class Contacts {
        [XmlArray("Contact")]
        [XmlArrayItem("Field", typeof(Values))]
        public Values[] Field { get; set; }
    }
    public class Locations {
        [XmlArray("Location")]
        [XmlArrayItem("Field", typeof(Values))]
        public Values[] Field { get; set; }
    }
    public class Values {
        [XmlAttribute("APIName")]
        public string APIName { get; set; }
        [XmlText]
        public string Value { get; set; }
    }  

(为了便于阅读,我已经省略了一些课程) 最大的问题是,是否有一种不同的方式反序列化为C#obj,或者正确的方法是手动进行映射?

修改 添加了修订的类结构,任何人都可以帮助我如何构建类?不确定如何处理Account中的所有Field = APIName元素?

1 个答案:

答案 0 :(得分:0)

您不在XML文件中提供Namespace

如果没有提供自定义命名空间,

XmlRootAttribute默认为:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

尝试告诉您的XmlReader不要使用命名空间

 XmlReader reader = new XmlReader() { Namespaces = false };

但是:我强烈建议您为文件使用特定的命名空间!