Reputation: 1529
Please Note that this application uses Silverlight5:
I have xml like this:
<A>
<B>
<C>C1</C>
<D>D1</D>
</B>
<B>
<E>E1</E>
<F>F1<F1>
</B>
</A>
I just have problem in the Parent class which is "A". How will i manage the reptition of "B" ? And how i will access the data node(child nodes) inside the both B on deserializing ? Because i will have the object of A class and with that object how would i differnceiate between the two "B" to acess "C1,D1,E1,F1" ?
My try to do this is :
[XmlRoot(ElementName = "A")]
public class Parameter
{
[XmlElement("B")] //But what to do for another "B"
public B B { get; set; }
}
Here is B class
[XmlRoot(ElementName = "B")]
public class Parameter
{
[XmlElement("C")]
public string Label { get; set; }
[XmlElement("D")]
public string Type { get; set; }
}
But what and how to do for E and F element? And how to acess C1,D1 and E1 AND F1 on derrialization?
EDIT after Andy Jones
,Suppose I try to get the object of "A" class like this:
XmlSerializer deserializer = new XmlSerializer(typeof(A));
XmlReader reader = XmlReader.Create(new StringReader(xmlstring));//HerexmlString contains my xml given above
A a = (A)deserializer.Deserialize(reader);
Now using the Object "a"
i want to access C1, D1 and C2,D2 . Then how to do that ?
Upvotes: 0
Views: 57
Reputation: 604
You will not be able to deal with this XML using Serialization because you have two different definitions for the B node.
The closest you'll be able to get is:
<A>
<B>
<C>C1</C>
<D>D1</D>
</B>
<B>
<C>C2</C>
<D>D2</D>
</B>
</A>
But first you'll need to sort out the C# classes. Firstly, you have two classes with the same name and an incorrect reference to "B".
Class A should look like this:
[XmlRoot(ElementName = "A")]
public class ParameterA
{
[XmlElement("B")]
public ParameterB B { get; set; }
}
...and class "B" should look like this:
[XmlRoot(ElementName = "B")]
public class ParameterB
{
[XmlElement("C")]
public string Label { get; set; }
[XmlElement("D")]
public string Type { get; set; }
}
That will allow you to get XML that look like:
<A>
<B>
<C>C1</C>
<D>D1</D>
</B>
</A>
Now for the repetition of "B". Modify ParamaterA to have a list of Bs:
[XmlRoot(ElementName = "A")]
public class ParameterA
{
[XmlElement("B")]
public List<ParameterB> B { get; set; }
}
This will allow you to create the XML above.
When you deserialize the XML to the classes you will be able to access the elements of B like this:
var firstParameter = a.B[0];
var secondParameter = a.B[1];
List<T>
allows you to access elements by index (just like an array).
You could also iterate over the values with foreach
:
foreach(ParameterB b in a.B)
{
...
}
Upvotes: 1
Reputation: 492
Minor correction to the XML:
<F>F1<F1>
should be
<F>F1</F>
Beyond that, it looks like the class A should have a collection of B elements:
public class A
{
public List<B> B { get; set; }
}
and your B class should have properties for C, D, E, and F:
public class B
{
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
public string F { get; set; }
}
Upvotes: 1