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"
Here’s a simple example based on the info you provided. Basically, you will need to make a separate class for
anotherElmentwhich contains astring.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 thatMyClasshas a document element calledrootto match your Xml. By default, the serializer will look for an element calledMyClass, and if you omit it, the deserialize method will throw.This should help get you going:
And a sample test method based on the Xml you provided: