chobo2
chobo2

Reputation: 85835

How do I make a serialization class for this?

I have something like this (sorry for the bad names)

   <?xml version="1.0" encoding="utf-8" ?>
    <root xmlns="http://www.domain.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.domain.com Schema.xsd">
      <product></product>
      <SomeHighLevelElement>
         <anotherElment>
              <lowestElement> </lowestElement>
         </anotherElment>
      </SomeHighLevelElement>
    </root>

I have something like this for my class

public class MyClass
{
    public MyClass()
    {
        ListWrapper= new List<UserInfo>();
    }


    public string product{ get; set; }


    public List<SomeHighLevelElement> ListWrapper{ get; set; }


}

public class SomeHighLevelElement
{

    public string lowestElement{ get; set; }
}

But I don't know how to write the code for the "anotherElement" not sure if I have to make another wrapper around it.

Edit

I know get a error in my actual xml file. I have this in my tag

xmlns="http://www.domain.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.Domain.com Schema.xsd

Throws an exception on the root line saying there was a error with this stuff. So I don't know if it is mad at the schemaLocation since I am using local host right now or what.

Error

System.InvalidOperationException was caught
  Message="There is an error in XML document (2, 2)."
  Source="System.Xml"

Upvotes: 0

Views: 463

Answers (1)

wsanville
wsanville

Reputation: 37516

Here's a simple example based on the info you provided. Basically, you will need to make a separate class for anotherElment which contains a string.

You can control exactly how your class parses the Xml elements using attributes, which basically maps your class properties to elements/attributes in the Xml file. So for example, since your document element in the sample Xml you provided is root, I explicitly define that MyClass has a document element called root to match your Xml. By default, the serializer will look for an element called MyClass, and if you omit it, the deserialize method will throw.

This should help get you going:

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("root")]
public class MyClass
{
    public MyClass()
    {

    }

    public string product { get; set; }

    [XmlElement("SomeHighLevelElement")]
    public List<SomeHighLevelElement> ListWrapper { get; set; }

}

public class SomeHighLevelElement
{
    public AnotherElment anotherElment { get; set; }
}

public class AnotherElment
{
    public string lowestElement { get; set; }
}

And a sample test method based on the Xml you provided:

using System.Xml;
using System.Xml.Serialization;
using System.IO;
.
.
.
public void Test()
{
    string xml = @"<root>
                  <product>product name</product>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>foo</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>bar</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                  <SomeHighLevelElement>
                    <anotherElment>
                      <lowestElement>baz</lowestElement>
                    </anotherElment>
                  </SomeHighLevelElement>
                </root>";
    MyClass c = Deserialize<MyClass>(xml);
}

public T Deserialize<T>(string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    return (T)serializer.Deserialize(new StringReader(xml));
}

Upvotes: 2

Related Questions