Need to create a .NET object that will be serialized into a specific XML format

G

Gasnic

Hi

I need to create a .NET object that will be serialized into the following xml structure ( I can't change the XML schema)

<Root>
<Record>
<Field1>data1</Field1>
<Field2>data2</Field2>
<Record>
<Record>
<Field1>data3</Field1>
<Field2>data4</Field2>
<Record>
</Root>


I tried to create a set of data class like this

Public Class Root
{
[XmlArray(ElementName="Record")]
[XmlArrayItem(Type=typeof(Record))]
public ArraryList Records;
}

Public Class Record
{
[XmlElement("Field1")]
public string Field1;

[XmlElement('Field2")]
public string Field2;
}



And inthe code I just create a serializer like the following and run the serialize method

XmlSerializer serializer = new XmlSerializer (typeof(Root));
serializer.serialize( myFileStream, myRootObj)

This way the serialized message become something like this

<Root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Record>
<Record>
<Field1>data1</Field1>
<Field2>data2</Field2>
<Record>
<Record>
<Field1>data3</Field1>
<Field2>data4</Field2>
<Record>
<Record>
<Root>

Which is not what I want as it has an extra Record node outside the repeating Record node.

Anyone has idea how can I create a .NET object to serialize to the xml struct at the top?

Any help is greatly appericated
 
G

Gasnic

I actually made it work by using xsd.exe, so I generated the class.

Now I am having another minor problem,

The Root node has the following attribute that I don't want

<Root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

is there anyway that I can get rid of these xmlns thing?

thanks

Hi

I need to create a .NET object that will be serialized into the following xml structure ( I can't change the XML schema)

<Root>
<Record>
<Field1>data1</Field1>
<Field2>data2</Field2>
<Record>
<Record>
<Field1>data3</Field1>
<Field2>data4</Field2>
<Record>
</Root>


I tried to create a set of data class like this

Public Class Root
{
[XmlArray(ElementName="Record")]
[XmlArrayItem(Type=typeof(Record))]
public ArraryList Records;
}

Public Class Record
{
[XmlElement("Field1")]
public string Field1;

[XmlElement('Field2")]
public string Field2;
}



And inthe code I just create a serializer like the following and run the serialize method

XmlSerializer serializer = new XmlSerializer (typeof(Root));
serializer.serialize( myFileStream, myRootObj)

This way the serialized message become something like this

<Root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Record>
<Record>
<Field1>data1</Field1>
<Field2>data2</Field2>
<Record>
<Record>
<Field1>data3</Field1>
<Field2>data4</Field2>
<Record>
<Record>
<Root>

Which is not what I want as it has an extra Record node outside the repeating Record node.

Anyone has idea how can I create a .NET object to serialize to the xml struct at the top?

Any help is greatly appericated
 
G

Guest

xmlns attributes are the XML namespaces used in the file and part of a good
XML document. Any other parser should understand them without an error.

Dont remove them if you dont have to.
 

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