By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

--
Gasnic
http://enggas.spaces.live.com/
Jan 30 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Gasnic" <ga****@gmail.comwrote in message news:O8**************@TK2MSFTNGP02.phx.gbl...
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

--
Gasnic
http://enggas.spaces.live.com/
Jan 30 '07 #2

P: n/a
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.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Gasnic" wrote:
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

"Gasnic" <ga****@gmail.comwrote in message news:O8**************@TK2MSFTNGP02.phx.gbl...
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

--
Gasnic
http://enggas.spaces.live
Jan 31 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.