如何反序列化具有相同名称的XMLElement

时间:2016-09-06 10:31:13

标签: c# .net xml-deserialization

我有XML根元素和XML元素同名,我不知道应该如何更改我的模型类

以下代码可以正常工作,因为XML元素不会重复使用相同的名称,在我的情况下,性别列表= 1

不能更改XML输出格式,因为它来自另一个系统,除非在C#代码级别过滤掉

XML

<?xml version="1.0"?>

 <Gender>
   <Gender list="1">
    <Item>
      <CODE>M</CODE>
      <DESCRIPTION>Male</DESCRIPTION> 
    </Item>
    <Item>
      <CODE>F</CODE>
      <DESCRIPTION>Female</DESCRIPTION>
    </Item>
   </Gender>
  </Gender>

模型类

 public class Gender
{

    [XmlElement("Item")]
    public List<Item> GenderList = new List<Item>();
}


public class Item
{
    [XmlElement("CODE")]
    public string Code { get; set; }

    [XmlElement("DESCRIPTION")]
    public string Description { get; set; }
}

XML解析类

public static class XMLPrasing
{
    public static Object ObjectToXML(string xml, Type objectType)
    {
        StringReader strReader = null;
        XmlSerializer serializer = null;
        XmlTextReader xmlReader = null;
        Object obj = null;
        try
        {
            strReader = new StringReader(xml);
            serializer = new XmlSerializer(objectType);
            xmlReader = new XmlTextReader(strReader);
            obj = serializer.Deserialize(xmlReader);
        }
        catch (Exception exp)
        {
            //Handle Exception Code
            var s = "d";
        }
        finally
        {
            if (xmlReader != null)
            {
                xmlReader.Close();
            }
            if (strReader != null)
            {
                strReader.Close();
            }
        }
        return obj;
    }

第二次更新

如果我使用不同的性别名称更改我的代码,那么这项工作,问题仍然是如何处理相同的名称

<?xml version="1.0"?>

<Gender>
 <GenderX list="1">
   <Item>
    <CODE>M</CODE>
    <DESCRIPTION>Male</DESCRIPTION>
   </Item>
   <Item>
    <CODE>F</CODE>
    <DESCRIPTION>Female</DESCRIPTION>
   </Item>
  </GenderX>
 </Gender>

模型类

  [XmlRoot("Gender")]
public class Gender
{

    [XmlElement("GenderX")]
    public List<GenderX> GenderXList = new List<GenderX>();       
}


public class GenderX
{
    [XmlElement("Item")]
    public List<Item> GenderList = new List<Item>();
}



public class Item
{
    [XmlElement("CODE")]
    public string Code { get; set; }

    [XmlElement("DESCRIPTION")]
    public string Description { get; set; }
}

3 个答案:

答案 0 :(得分:0)

我找到了答案

[XmlRoot("Gender")]
public class Gender
{

    [XmlElement("Gender")]
    public List<GenderListWrap> _GenderListWrap = new List<GenderListWrap>();       
}


public class GenderListWrap
{
    [XmlAttribute("list")]
    public string _ListTag { get; set; }

    [XmlElement("Item")]
    public List<Item> _GenderList = new List<Item>();
}



public class Item
{
    [XmlElement("CODE")]
    public string Code { get; set; }

    [XmlElement("DESCRIPTION")]
    public string Description { get; set; }
}

答案 1 :(得分:0)

如果您只有一个顶级的Gender元素,那么这就足够了:

[XmlRoot(ElementName = "Gender")]
public class Genders
{
    [XmlElement(ElementName = "Gender")]
    public Gender gender { get; set; }
}

public class Gender
{
    [XmlElement(ElementName = "Item")]
    public List<Item> GenderList = new List<Item>();
}


public class Item
{
    [XmlElement("CODE")]
    public string Code { get; set; }

    [XmlElement("DESCRIPTION")]
    public string Description { get; set; }
}

答案 2 :(得分:0)

我使用Generic方法反序列化XMLString 此方法采用xml字符串和反序列化的发送方模型Type。

你必须使用Model deserialized xml这样的属性,你不应忘记为类属性写[Serializable],为属性属性写[XmlElement]

  [Serializable]
  public class Gender
{

    [XmlElement("Item")]
    public List<Item> GenderList = new List<Item>();
}


public class Item
{
    [XmlElement("CODE")]
    public string Code { get; set; }

    [XmlElement("DESCRIPTION")]
    public string Description { get; set; }
}

 public static T Deserialize<T>(string input) where T : class
        {
            Log.Debug("Deserialize" + typeof(T).Name, "xml string Deserialize ediliyor" + Environment.NewLine + input);            

            XmlSerializer ser = new XmlSerializer(typeof(T), "SetDefaultNamespace"); // optinal parameters DefaultNamespace
            using (StringReader sr = new StringReader(input))
            {
                var desearializedObject = (T)ser.Deserialize(sr);
                Log.Debug("Deserialize" + typeof(T).Name, "Obje Deserialize işlemi tamamlandı");
                return desearializedObject;
            }
        }


    Deserialize<Gender>(xmlString);