XML deserialization

G

gavkel

Hi all

Im building an object model so I can deserialize my incoming XML and
Ive got a wee problem

My incoming XML looks like this

....
<Surnames>
<Surname>Kelly</Surname>
<AlternativeSurnames>
<AlternativeSurname>Hill</AlternativeSurname>
<AlternativeSurname>Bell</AlternativeSurname>
</AlternativeSurnames>
</Surnames>
....

Have 3 classes - AlternativeSurname, AlternativeSurnamesCollection and
Surnames.

namespace PROMOD
{
using System;
using System.Xml;
using System.Xml.Serialization;

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://Schemas.SearchCommon")]

public class AlternativeSurname
{
public AlternativeSurname()
{
}

[System.Xml.Serialization.XmlElementAttribute("AlternativeSurname")]
public string Name;
}
}


namespace PROMOD
{
using System;
using System.Collections;
using System.Xml.Serialization;

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://Schemas.SearchResponse")]

public class AlternativeSurnamesCollection : CollectionBase
{
public AlternativeSurnamesCollection()
{
}

public void Add(AlternativeSurname alternativeSurname)
{
this.List.Add(alternativeSurname);
}

public void Remove(AlternativeSurname alternativeSurname)
{
this.List.Remove(alternativeSurname);
}

public AlternativeSurname this[int index]
{
get { return (AlternativeSurname)this.List[index]; }
set { this.List[index] = value; }
}
}
}


namespace PROMOD
{
using System;
using System.Xml.Serialization;

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://Schemas.SearchCommon")]

public class Surnames
{
public Surnames()
{
}

public string Surname;


public AlternativeSurnamesCollection AlternativeSurnames;
}
}

Now, my problem is the AlternativeSurname element is not being picked
up because it has no matching variable in the AlternativeSurname class
- ive called it name because I cant use AlternativeSurname as this is
the same name as the class. Ive investigated into some form of XML
declaration (ie:
[System.Xml.Serialization.XmlElementAttribute("AlternativeSurname")])
but no joy. Do you know the answer?

Best regards and good luck
Gav
 
G

gavkel

No I also used the XSD tool to generate the classes but Im going
through a process of 'beautifing' them - adding collection classes and
such but htis is still an issue in the original xsd generated class.
Hi,
Are you writing this xml class by hand ?

What I did was make an xml file, generate a xsd from it through visual
studio.net and use that xsd to generate the C# class from xsd.exe tool and
do serialization using that class.

Ab.
http://joehacker.blogspot.com


gavkel said:
Hi all

Im building an object model so I can deserialize my incoming XML and
Ive got a wee problem

My incoming XML looks like this

...
<Surnames>
<Surname>Kelly</Surname>
<AlternativeSurnames>
<AlternativeSurname>Hill</AlternativeSurname>
<AlternativeSurname>Bell</AlternativeSurname>
</AlternativeSurnames>
</Surnames>
...

Have 3 classes - AlternativeSurname, AlternativeSurnamesCollection and
Surnames.

namespace PROMOD
{
using System;
using System.Xml;
using System.Xml.Serialization;

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://Schemas.SearchC
ommon")]

public class AlternativeSurname
{
public AlternativeSurname()
{
}

[System.Xml.Serialization.XmlElementAttribute("AlternativeSurname")]
public string Name;
}
}


namespace PROMOD
{
using System;
using System.Collections;
using System.Xml.Serialization;

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://Schemas.SearchR
esponse")]

public class AlternativeSurnamesCollection : CollectionBase
{
public AlternativeSurnamesCollection()
{
}

public void Add(AlternativeSurname alternativeSurname)
{
this.List.Add(alternativeSurname);
}

public void Remove(AlternativeSurname alternativeSurname)
{
this.List.Remove(alternativeSurname);
}

public AlternativeSurname this[int index]
{
get { return (AlternativeSurname)this.List[index]; }
set { this.List[index] = value; }
}
}
}


namespace PROMOD
{
using System;
using System.Xml.Serialization;

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://Schemas.SearchC
ommon")]

public class Surnames
{
public Surnames()
{
}

public string Surname;


public AlternativeSurnamesCollection AlternativeSurnames;
}
}

Now, my problem is the AlternativeSurname element is not being picked
up because it has no matching variable in the AlternativeSurname class
- ive called it name because I cant use AlternativeSurname as this is
the same name as the class. Ive investigated into some form of XML
declaration (ie:
[System.Xml.Serialization.XmlElementAttribute("AlternativeSurname")])
but no joy. Do you know the answer?

Best regards and good luck
Gav
 
G

gavkel

Added this declaration to the top of the AlternativeSurname class -
still no joy... Shame because this is the sort of thing Im expecting to
sort it
 
A

Abubakar

Hi,
Are you writing this xml class by hand ?

What I did was make an xml file, generate a xsd from it through visual
studio.net and use that xsd to generate the C# class from xsd.exe tool and
do serialization using that class.

Ab.
http://joehacker.blogspot.com
 
A

Abubakar

It does and this worked for me few days back when I was working on something
similar. I havnt looked at the tweakings that u have done to the tool
generated class, but r u sure you havnt messed up the object layout or
something that the deserializer is having a clash with, something that it
may not be reporting.

Ab.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top