473,378 Members | 1,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,378 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
3 1151
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
On Jan 30, 5:30 pm, "Gasnic" <gas...@gmail.comwrote:
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?
If you must get rid of them, use the following code:

using (StreamWriter sw = new StreamWriter(@"filename.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(SomeClass));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

xs.Serialize(sw, obj, ns);
}

The docs say that using the XmlSerializerNamespaces in this manner is
not support and that you cant use code in that manner, but it seems to
work.
Jan 31 '07 #4

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...
5
by: Samuel R. Neff | last post by:
Are there any tools available to view a file containing a binary serialized object in a friendly format? Something to list classes and data and such? Thanks, Sam B-Line is now hiring one...
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...
2
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.