473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
2 1283
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher Dedels | last post by:
Is it possible to deserialize an object which was serialized in c/c++? If so, how? Regards, Chris
2
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server...
3
by: GoodMorningSky | last post by:
I have long term question about object serialization. Object serialization is used in many ways. In .net I heard XML is used for object serialization. I understand how object values are serialized...
0
by: Pierre | last post by:
Hi, I'm trying to select specific nodes from a XmlDocument filled with a serialized object and to insert these nodes into another XmlDocument. The object is well serialized (see below). From a...
2
by: ddt | last post by:
Hi, I have a class with some properties. When I return the object from Web Services, it automatically serialized to XML format. My question is how can I get that serialized XML of the object (not...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
3
by: Gasnic | last post by:
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>...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.