-3

I have following XML:

        xml += "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>";
        xml += "<root>";
        xml += " <success>true</success>";
        xml += " <data>";
        xml += "   <item>";
        xml += "     <StateId>0</StateId>";
        xml += "     <StateName>Test</StateName>";
        xml += "     <GroupId>0</GroupId>";
        xml += "   </item>";
        xml += " </data>";
        xml += "</root>";

Basically I am interested in the root->data->item element only and I am trying to get it:

public static T ConvertXmlToClass<T>(string xml)
{
    XmlRootAttribute xRoot = new XmlRootAttribute();
    xRoot.ElementName = "root";
    xRoot.IsNullable = true;
    var serializer = new XmlSerializer(typeof(T), xRoot);
    return (T)serializer.Deserialize(new StringReader(xml));
}

I am getting empty object, as the actual data is in root->data->item.

I've tried to change xRoot.ElementName = "root"; to xRoot.ElementName = "root/data"; or xRoot.ElementName = "root/data/item"; but I've got:

InnerException: System.InvalidOperationException HResult=-2146233079 Message= was not expected. Source=Microsoft.GeneratedCode

The object class is:

public partial class DocumentStatus
{
    public DocumentStatus()
    {
        this.Documents = new HashSet<Document>();
        this.DocumentsTrackings = new HashSet<DocumentsTracking>();
        this.DocumentsTrackingChildDocuments = new HashSet<DocumentsTrackingChildDocument>();
    }
    [XmlElement("StateId")]
    public int StateId { get; set; }
    [XmlElement("StateName")]
    public string StateName { get; set; }
    [XmlElement("GroupId")]
    public Nullable<int> GroupId { get; set; }

    public virtual HashSet<Document> Documents { get; set; }
    public virtual HashSet<DocumentsTracking> DocumentsTrackings { get; set; }
    public virtual HashSet<DocumentsTrackingChildDocument> DocumentsTrackingChildDocuments { get; set; }
}    
Dmitrij Kultasev
  • 5,447
  • 5
  • 44
  • 88
  • What is unclear `The 'data' start tag on line 1 position 73 does not match the end tag of 'root'. Line 1, position 167.` – Eser Sep 09 '15 at 07:12

1 Answers1

2

You should fix your classes so that your serialization will be valid:

[Serializable()]
[System.Xml.Serialization.XmlRoot("root")]
public class SomeClass
{
     public Data()
    {
        DataArray = new List<Data>(); 
    }

    [XmlElement("data", typeof(Data))]
    public List<Data> DataArray { get; set; }
} 

[Serializable()]
public class Data
{
    public Data()
    {
        DocumentStatusArray = new List<DocumentStatus>(); 
    }

    [XmlElement("item", typeof(DocumentStatus))]
    public List<DocumentStatus> DocumentStatusArray { get; set; }
} 

[Serializable()]
public class DocumentStatus
{
    [System.Xml.Serialization.XmlAttribute("StateId")]
    public int StateId {get; set;}

    [System.Xml.Serialization.XmlAttribute("StateName")]
    public string StateName { get; set; }

    [System.Xml.Serialization.XmlAttribute("GroupId")]
    public GroupId groupId { get; set; } 
} 
yglodt
  • 13,807
  • 14
  • 91
  • 127
Yael
  • 1,566
  • 3
  • 18
  • 25