Click here to Skip to main content
15,885,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How deserialize document XML:
---------------------------------------------
HTML
<something id=""234"">
     <items>
          <item name=""Obj1"">
               <a>1</a>
               <k>1</k>
          </item>
          <item name=""Obj2"">
                <c>5</c>
                <d>2</d>
                <e>1</e>
           </item>
     </items>
</something>

I can't get value elemnts in item
---------------------------------------------
C#
[XmlRoot("something")]
    public class Something: ClassBase
    {
       [XmlAttribute("id")]
        public string id;

       [XmlArray("items")]
       public ItemsCollection forms { set; get; } 

       public Something(): base() { }
    }


    [XmlType("item")]
    public class ItemsCollection: CollectionClassBase<Item>
    {
        public ItemsCollection() : base() { }
    }


    [XmlType("item")]
    public class Item: ClassBase
    {
        [XmlAttribute("name"), DefaultValue("")]
        public string name { set; get; }
    }

    [XmlType("item")]
    public class ItemObj1 : Item
    {
        [XmlElement("a"), DefaultValue("")]
        public string a  { set; get; }
        [XmlElement("b"), DefaultValue("")]
        public string b { set; get; }
    }
Posted

1 solution

I'd suggest to use xsd.exe tool[^] which can generate proper class(es) definition from xml.

Follow these steps: Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization[^]

[EDIT]
I'd suggest to change your XML structure:
XML
<something id="234">
     <items>
          <item name="Obj1">
               <a>1</a>
               <k>1</k>

to:
XML
<something id="234">
     <items>
          <item name="Obj1">
               <property name="a">1</a>
               <property name="k">1</k>


It should help you while XML normalization.
[/EDIT]
 
Share this answer
 
v2
Comments
Member 10294936 18-Mar-15 10:59am    
Thanks for your reaply.
I use xsd.exe tool. After create XSD I see that item have all tag a, k, c, d, e.
So I create class and use it:

[XmlType("item")]
public class Item: ClassBase
{
[XmlAttribute("name"), DefaultValue("")]
public string name { set; get; }
[XmlElement("a"), DefaultValue(0)]
public int a { set; get; }
[XmlElement("k"), DefaultValue(0)]
public int k { set; get; }
[XmlElement("c"), DefaultValue(0)]
public int c { set; get; }
[XmlElement("d"), DefaultValue(0)]
public int d { set; get; }
[XmlElement("d"), DefaultValue(0)]
public int d { set; get; }
}

This approach isn't good for me, because in my XML is maybe 1000 fields with value.

BTW My console application deserialization file XML that I receive to my request. This is a cyclical process. Next time I mapping objects to tables in database and save data.
Maciej Los 18-Mar-15 11:57am    
I think you should change XML structure (and related class) - if you can...
Please, see updated answer ;)
Member 10294936 18-Mar-15 11:06am    
-- duplicate --
Member 10294936 19-Mar-15 7:01am    
I can't change XML (only receve from another application).
XSD.exe is helpful for me, because in XML is to many fileds that create class manualy.
Next time I save data to database use TableAdapters.

Thanks a lot, regards, pozdrawiam :)
Małgorzata
Maciej Los 19-Mar-15 7:30am    
You're very welcome ;)
Cheers, Pozdrawiam, Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900