2

I have tried everything cant deserialize this XML document.

<items>
    <item>
        <id>00175565</id>
        <descr>KJAAM-EMC 16x(2+1)x0,5 T500</descr>
        <unit>st</unit>
        <vat>25</vat>
        <inprice>374</inprice>
        <isstock>0</isstock>
        <stock>0</stock>
        <paccount/>
        <ean>00175565</ean>
        <type>1</type>
        <producer/>
        <producer_itemno>00175565</producer_itemno>
        <package_height/>
        <package_depth/>
        <package_width/>
        <package_weight/>
        <stock_place/>
        <stock_warning/>
        <note/>
        <bulky>0</bulky>
        <omit>0</omit>
        <available>0</available>
        <account>3041</account>
        <constracct>3231</constracct>
        <exportacct>3315</exportacct>
        <eurevacct>3318</eurevacct>
        <euvatacct>3316</euvatacct>
        <supplierno/>
        <show_in_webshop>0</show_in_webshop>
        <price>
              <list-a>
                    <from-0>486.2</from-0>
              </list-a>
        </price>
    </item>
</items>

This is how i deserialize it

[Serializable()]
public class item
{
    [System.Xml.Serialization.XmlElement("id")]
    public string ID { get; set; }

    [System.Xml.Serialization.XmlElement("descr")]
    public string Descr { get; set; }

    [System.Xml.Serialization.XmlElement("unit")]
    public string Unit { get; set; }

    [System.Xml.Serialization.XmlElement("vat")]
    public string Vat { get; set; }

    [System.Xml.Serialization.XmlElement("inprice")]
    public string Inprice { get; set; }

    [System.Xml.Serialization.XmlElement("isstock")]
    public string Isstock { get; set; }

    [System.Xml.Serialization.XmlElement("stock")]
    public string Stock { get; set; }

    [System.Xml.Serialization.XmlElement("paccount")]
    public string Paccount { get; set; }

    [System.Xml.Serialization.XmlElement("ean")]
    public string Ean { get; set; }

    [System.Xml.Serialization.XmlElement("type")]
    public string Type { get; set; }

    [System.Xml.Serialization.XmlElement("producer")]
    public string Producer { get; set; }

    [System.Xml.Serialization.XmlElement("producer_itemno")]
    public string Producer_itemno { get; set; }

    [System.Xml.Serialization.XmlElement("package_height")]
    public string Package_height { get; set; }

    [System.Xml.Serialization.XmlElement("package_depth")]
    public string Package_depth { get; set; }

    [System.Xml.Serialization.XmlElement("package_width")]
    public string Package_width { get; set; }

    [System.Xml.Serialization.XmlElement("package_weight")]
    public string Package_weight { get; set; }

    [System.Xml.Serialization.XmlElement("stock_place")]
    public string Stock_place { get; set; }

    [System.Xml.Serialization.XmlElement("stock_warning")]
    public string Stock_warning { get; set; }

    [System.Xml.Serialization.XmlElement("note")]
    public string Note { get; set; }

    [System.Xml.Serialization.XmlElement("bulky")]
    public string Bulky { get; set; }

    [System.Xml.Serialization.XmlElement("omit")]
    public string Omit { get; set; }

    [System.Xml.Serialization.XmlElement("available")]
    public string Available { get; set; }

    [System.Xml.Serialization.XmlElement("account")]
    public string Account { get; set; }

    [System.Xml.Serialization.XmlElement("constracct")]
    public string Constracct { get; set; }

    [System.Xml.Serialization.XmlElement("exportacct")]
    public string Exportacct { get; set; }

    [System.Xml.Serialization.XmlElement("eurevacct")]
    public string Eurevacct { get; set; }

    [System.Xml.Serialization.XmlElement("euvatacct")]
    public string Euvatacct { get; set; }

    [System.Xml.Serialization.XmlElement("supplierno")]
    public string Supplierno { get; set; }

    [System.Xml.Serialization.XmlElement("show_in_webshop")]
    public string Show_in_webshop { get; set; }

    [XmlArray("price")]
    [XmlArrayItem("list-a")]
    public List<string> price { get; set; }
}


[XmlRoot("items")]
public class items
{
    [XmlElement("item")]
    public item[] item { get; set; }
}

The problem is that i get one row of the first set of item. but when i remove

    [XmlArray("price")]
    [XmlArrayItem("list-a")]
    public List<string> price { get; set; }

it gives me all the rows. I have tried alot of things and look on some other posted questions equal to this but i cant get it to work.

the only way for me to get something at all from is to use the code above.

I greatly appreciate all the help i can get!

4
  • This might be totally wrong, but it looks like you have a typo in your code. You say "[XmlArrayItem("list-a"]" which is missing a paren. I would assume this can't compile, but maybe it just throws a runtime error instead. Can you try fixing that, or is that not a problem in the actual code? Commented Oct 8, 2012 at 16:38
  • its wrong it should be [XmlArrayItem("list-a")] but thats not the problem. Commented Oct 8, 2012 at 16:43
  • This is just a guess on an example I saw, but try using "[XmlArrayItem("list-a", typeof(string))]" Commented Oct 8, 2012 at 16:51
  • oh wait, I just realized the issue. You store your price in a weird way. You are telling your program to retrieve the price value from the list-a node, when really you are storing it in from-0. What you should do is either remove the from-0 node, or add it to your specification. Commented Oct 8, 2012 at 16:59

1 Answer 1

6

Try this. It is working. I have tested it:

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class items
{
    public itemsItem item { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItem
{
    public uint id { get; set; }
    public string descr { get; set; }

    public string unit { get; set; }

    public byte vat { get; set; }

    public ushort inprice { get; set; }

    public byte isstock { get; set; }

    public byte stock { get; set; }

    public object paccount { get; set; }

    public uint ean { get; set; }

    public byte type { get; set; }

    public object producer { get; set; }

    public uint producer_itemno { get; set; }

    public object package_height { get; set; }

    public object package_depth { get; set; }

    public object package_width { get; set; }

    public object package_weight { get; set; }

    public object stock_place { get; set; }

    public object stock_warning { get; set; }

    public object note { get; set; }

    public byte bulky { get; set; }

    public byte omit { get; set; }

    public byte available { get; set; }

    public ushort account { get; set; }

    public ushort constracct { get; set; }

    public ushort exportacct { get; set; }

    public ushort eurevacct { get; set; }

    public ushort euvatacct { get; set; }

    public object supplierno { get; set; }

    public byte show_in_webshop { get; set; }

    public itemsItemPrice price { get; set; }

}


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItemPrice
{
    [System.Xml.Serialization.XmlElementAttribute("list-a")]
    public itemsItemPriceLista lista { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItemPriceLista
{

    private decimal from0Field;

    [System.Xml.Serialization.XmlElementAttribute("from-0")]
    public decimal from0 { get; set; }

}

and the deserialization code:

string xml = @"<items>
            <item>
                <id>00175565</id>
                <descr>KJAAM-EMC 16x(2+1)x0,5 T500</descr>
                <unit>st</unit>
                <vat>25</vat>
                <inprice>374</inprice>
                <isstock>0</isstock>
                <stock>0</stock>
                <paccount/>
                <ean>00175565</ean>
                <type>1</type>
                <producer/>
                <producer_itemno>00175565</producer_itemno>
                <package_height/>
                <package_depth/>
                <package_width/>
                <package_weight/>
                <stock_place/>
                <stock_warning/>
                <note/>
                <bulky>0</bulky>
                <omit>0</omit>
                <available>0</available>
                <account>3041</account>
                <constracct>3231</constracct>
                <exportacct>3315</exportacct>
                <eurevacct>3318</eurevacct>
                <euvatacct>3316</euvatacct>
                <supplierno/>
                <show_in_webshop>0</show_in_webshop>
                <price>
                      <list-a>
                            <from-0>486.2</from-0>
                      </list-a>
                </price>
            </item>
        </items>
";

            XmlSerializer serializer = new XmlSerializer(typeof(items));
            using (StringReader stringReader = new StringReader(xml))
            {
                using (XmlTextReader textReader = new XmlTextReader(stringReader))
                {
                   items items =  (items)serializer.Deserialize(textReader);
                }
            }

Edit:

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class items
{
    [XmlElementAttribute("item")]
    public itemsItem[] item { get; set; }
}

Hopefully final edit :)

Based on your .xml, try this. It works with: http://www.kbbs.se/get_item.xml I've tested it.

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class items
{

    [System.Xml.Serialization.XmlElementAttribute("item")]
    public itemsItem[] item {get; set;}    
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItem
{

    public string id{get; set;} 

    public string descr{get; set;} 

    public string unit{get; set;} 

    public string vat{get; set;} 

    public decimal inprice{get; set;} 

    public byte isstock{get; set;} 

    public sbyte stock{get; set;} 

    public object paccount{get; set;} 

    public string ean{get; set;} 

    public byte type{get; set;} 

    public string producer{get; set;} 

    public string producer_itemno{get; set;} 

    public object package_height{get; set;} 

    public object package_depth{get; set;} 

    public object package_width{get; set;} 

    public object package_weight{get; set;} 

    public object stock_place{get; set;} 

    public string stock_warning{get; set;} 

    public object note{get; set;} 

    public byte bulky{get; set;} 

    public byte omit{get; set;} 

    public sbyte available{get; set;} 

    public ushort account{get; set;} 

    public ushort constracct{get; set;} 

    public ushort exportacct{get; set;} 

    public ushort eurevacct{get; set;} 

    public ushort euvatacct{get; set;} 

    public object supplierno{get; set;} 

    public byte show_in_webshop{get; set;} 

    public itemsItemPrice price{get; set;} 

}


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItemPrice
{   
    [System.Xml.Serialization.XmlElementAttribute("list-")]
    public itemsItemPriceList list{get; set;} 

    [System.Xml.Serialization.XmlElementAttribute("list-a")]
    public itemsItemPriceLista lista{get; set;} 

    [System.Xml.Serialization.XmlElementAttribute("list-w")]
    public itemsItemPriceListw listw { get; set; } 

}


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItemPriceList
{   
    [System.Xml.Serialization.XmlElementAttribute("from-")]
    public object from{get; set;} 

}


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItemPriceLista
{
    [System.Xml.Serialization.XmlElementAttribute("from-0")]
    public decimal from0{get; set;} 

}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class itemsItemPriceListw
{
    [System.Xml.Serialization.XmlElementAttribute("from-0")]
    public byte from0{get; set;} 

}

Good luck :)

Sign up to request clarification or add additional context in comments.

3 Comments

It worked until i tried to make itemsItem into an array public itemsItem[] item { get; set; } <items> contains about 1000 <item> so it needs to be an array but that didnt work.
still cant get it to work. kbbs.se/get_item.xml thats the url to the file am working with. Thank you guys for helping me! i realy appreciate it.
@KristoferBrigelius: Welcome. Please see my last (and hopefully final ;)) edit. By the way, if this one helps you out, please click on up arrow where you see "2" and accept the answer. It encourages us to solve issues :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.