0

I have the following xml file that I need to read into my C# project so that I have a collection together with access to the attributes. I've tried several attempts although I'm not getting very far. I would like to use classes to represent the xml which I've seen before but cannot replicate. I don't want to read the file into a streamreader and then pull out the values. The previous project I think used the [Serializable] attribute on a class.

Hope this helps? Let me know if you need more, I'm using C# 4.0.

Thanks, James

<MyProducts xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<MyProduct MyProductCode="MBB" MyProductCategory="Computer" MyProductMaxNumber = "4">
  <MyProductLookups>
    <MyProductLookup Lang="AAA">Test 1</MyProductLookup>
    <MyProductLookup Lang="BBB">Test 2</MyProductLookup>
    <MyProductLookup Lang="CCC">Test 3</MyProductLookup>
    <MyProductLookup Lang="DDD">Test 4</MyProductLookup>
  </MyProductLookups>
</MyProduct>    
<MyProduct MyProductCode="LJJ" MyProductCategory="Laptop" MyProductMaxNumber = "4">
  <MyProductLookups>
    <MyProductLookup Lang="AAA">Test 5</MyProductLookup>
    <MyProductLookup Lang="BBB">Test 6</MyProductLookup>
    <MyProductLookup Lang="CCC">Test 7</MyProductLookup>
    <MyProductLookup Lang="DDD">Test 8</MyProductLookup>
  </MyProductLookups>
</MyProduct>
James Radford
  • 1,815
  • 4
  • 25
  • 40

2 Answers2

0

Take a look at xsd.exe to generate a schema for your XML file and the classes.

See Generate classes from XSD and Using XSD Tool to Generate Classes from XML

Community
  • 1
  • 1
Matthias
  • 12,053
  • 4
  • 49
  • 91
  • I don't believe we used any tool previously, just C# code to serialise the xml into objects(?) – James Radford Mar 14 '12 at 09:43
  • Yes - the tool will generate the code of classes representing the structure of the XML file for you so you don't have to write them. Take a look at the second link. – Matthias Mar 14 '12 at 09:47
  • I think the issue is how to parse them, nice tool though. – ntziolis Mar 14 '12 at 09:54
  • @ntziolis: The XML file can then be parsed using the XMLSerializer (resulting in objects of the generated classes) as shown in the second link. – Matthias Mar 14 '12 at 09:55
  • @ntziolis: Yes. This can be done using the tool, too (with maybe some manual corrections). – Matthias Mar 14 '12 at 10:02
  • cool. Im reading through it now.. My colleague mentioned this http://xsd2code.codeplex.com/ as well to create the classes although my understanding from what your comments have said is that the XSD tool should do that for me(?) – James Radford Mar 14 '12 at 10:24
  • Using XSD Tool to Generate Classes from XML is awesome, seems to work at first glance. many thanks – James Radford Mar 14 '12 at 15:29
0

The serializable thing only works properly when the convetions match.

Recently I have started to use LINQ to Xml more and more, since it let's me do exactly what I want:

var productsXml = XElement.Load(pathXmlFile);
var products = products.Elements("MyProduct").Select(product
    select new Product
    {
        MyProductCode = (string)product.Attribute("MyProductCode"),
        MyProductCategory = (string)product.Attribute("MyProductCategory"),
        MyProductMaxNumber  = (int)product.Attribute("MyProductMaxNumber "),
        MyProductLookups = product.Elements("MyProductLookups")
            .Elements("MyProductLookup").Select(lookup =>
            {
                new MyProductLookup()
                {
                    Lang = (string)lookup.Attribute("Lang"),
                    Value = (string)lookup
                }
            } 
    }

So you end up with a IEnumerable<Product> and can take it from there.

ntziolis
  • 10,091
  • 1
  • 34
  • 50
  • thanks, let me see how I get on with the XSD Tool and I'll get back to you if I need to investigate a different approach. Many thanks for the response. – James Radford Mar 14 '12 at 15:30