473,386 Members | 1,736 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,386 software developers and data experts.

strugling with Atributed XML parsing.

I have XML in following format
<ORDER>
<HEADER> ... </HEADER>
<LINE> ... <LINE>
<LINE> ... <LINE>
<LINE> ... <LINE>
</ORDER>

Can someone suggest how will i parseout such document using XmlSerializer
and attributes.
There can be different amount of lines in different documents.

I have

class clsOrder

{

clsHeader _header;

clsLine [] Lines;

}

How do i specify that <line> goes into Lines array?

Thanks.

George.
Nov 18 '05 #1
6 1165
Hi George,
Thanks for posting in the community!
From your description, you need some suggestions or information on how to
specify the proper XmlAttributes in certian classes so as to use
XmlSerializer to output them and the certain custom class you made contains
an array member of another custom class, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I suggest that you use the "XmlArray" and
XmlArrayItem" attribute to implement the control of the array member within
a certain class's serialization. For example, we can provide the class's
code as below:
public class clsLine
{
public clsLine()
{}

public string Name = "name";
public string Description = "description";
}

public class clsOrder
{
public clsOrder()
{}

[XmlElement("MyHeader")]
public string Header = "header";

[XmlArrayItem(typeof(clsLine),ElementName = "MyLine")]
[XmlArray(ElementName = "MyLines",Namespace = "", IsNullable = true)]
public clsLine[] Lines = null;

}

Notice the two attributes specified in the clsOrder class.
1. the "XmlArray" has specified what name to use for the array member when
the class's instance is serialized, here I specify it as "MyLines", and the
"XmlArrayItem" can be used to specify the array member's item's serialize
options, and when i serialzie a certain clsOrder instance via the following
code:
----------------------------------------------------
XmlSerializer serializer = new XmlSerializer(typeof(clsOrder));

clsOrder odr = new clsOrder();
odr.Header = "Test Order";
clsLine[] lines = new clsLine[5];

for(int i=0;i<lines.Length;i++)
{
clsLine line = new clsLine();
line.Name = "Name" + i.ToString();
line.Description = "Description" + i.ToString();
lines[i] = line;
}

odr.Lines = lines;

TextWriter writer = new StreamWriter("output.xml");
serializer.Serialize(writer,odr);
writer.Close();
-------------------------------------
The output.xml is like:
-------------------------------output.xml-------------------------
<?xml version="1.0" encoding="utf-8"?>
<clsOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyHeader>Test Order</MyHeader>
<MyLines>
<MyLine>
<Name>Name0</Name>
<Description>Description0</Description>
</MyLine>
<MyLine>
<Name>Name1</Name>
<Description>Description1</Description>
</MyLine>
<MyLine>
<Name>Name2</Name>
<Description>Description2</Description>
</MyLine>
<MyLine>
<Name>Name3</Name>
<Description>Description3</Description>
</MyLine>
<MyLine>
<Name>Name4</Name>
<Description>Description4</Description>
</MyLine>
</MyLines>
</clsOrder>
----------------------------------------------------

In addition, here is some tech references in MSDN on the how to use and
control the XmlSerializer, I believe they'll be helpful to you:

#Introducing XML Serialization
http://msdn.microsoft.com/library/en...oducingxmlseri
alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/en...ampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en...rollingseriali
zationbyxmlserializerwithattributes.asp?frame=true

#Attributes That Control XML Serialization
http://msdn.microsoft.com/library/en...ibutesthatcont
rolserialization.asp?frame=true

Please check out my preceding suggestions, if you have any questions on
them, please feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #2
That is the problem.
Your sample creates following XML
<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLines>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
</MyLines>
</clsOrder>

Unfortunately i need following structure.

<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
<MyLine> ..</MyLine>
</clsOrder>

Notice there is no <MyLines> tag

I do agree that your structure is more natural and i would not even ask my
question if that was a case. But i stuck with aftermarket standart which was
created by some guys with jumbo heads (joke) and need to be able to parse
that XML

Thanks.
George.

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:Da**************@cpmsftngxa07.phx.gbl...
Hi George,
Thanks for posting in the community!
From your description, you need some suggestions or information on how to
specify the proper XmlAttributes in certian classes so as to use
XmlSerializer to output them and the certain custom class you made contains an array member of another custom class, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I suggest that you use the "XmlArray" and
XmlArrayItem" attribute to implement the control of the array member within a certain class's serialization. For example, we can provide the class's
code as below:
public class clsLine
{
public clsLine()
{}

public string Name = "name";
public string Description = "description";
}

public class clsOrder
{
public clsOrder()
{}

[XmlElement("MyHeader")]
public string Header = "header";

[XmlArrayItem(typeof(clsLine),ElementName = "MyLine")]
[XmlArray(ElementName = "MyLines",Namespace = "", IsNullable = true)]
public clsLine[] Lines = null;

}

Notice the two attributes specified in the clsOrder class.
1. the "XmlArray" has specified what name to use for the array member when
the class's instance is serialized, here I specify it as "MyLines", and the "XmlArrayItem" can be used to specify the array member's item's serialize
options, and when i serialzie a certain clsOrder instance via the following code:
----------------------------------------------------
XmlSerializer serializer = new XmlSerializer(typeof(clsOrder));

clsOrder odr = new clsOrder();
odr.Header = "Test Order";
clsLine[] lines = new clsLine[5];

for(int i=0;i<lines.Length;i++)
{
clsLine line = new clsLine();
line.Name = "Name" + i.ToString();
line.Description = "Description" + i.ToString();
lines[i] = line;
}

odr.Lines = lines;

TextWriter writer = new StreamWriter("output.xml");
serializer.Serialize(writer,odr);
writer.Close();
-------------------------------------
The output.xml is like:
-------------------------------output.xml-------------------------
<?xml version="1.0" encoding="utf-8"?>
<clsOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyHeader>Test Order</MyHeader>
<MyLines>
<MyLine>
<Name>Name0</Name>
<Description>Description0</Description>
</MyLine>
<MyLine>
<Name>Name1</Name>
<Description>Description1</Description>
</MyLine>
<MyLine>
<Name>Name2</Name>
<Description>Description2</Description>
</MyLine>
<MyLine>
<Name>Name3</Name>
<Description>Description3</Description>
</MyLine>
<MyLine>
<Name>Name4</Name>
<Description>Description4</Description>
</MyLine>
</MyLines>
</clsOrder>
----------------------------------------------------

In addition, here is some tech references in MSDN on the how to use and
control the XmlSerializer, I believe they'll be helpful to you:

#Introducing XML Serialization
http://msdn.microsoft.com/library/en...oducingxmlseri alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/en...ampleofxmlseri alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en...rollingseriali zationbyxmlserializerwithattributes.asp?frame=true

#Attributes That Control XML Serialization
http://msdn.microsoft.com/library/en...ibutesthatcont rolserialization.asp?frame=true

Please check out my preceding suggestions, if you have any questions on
them, please feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3
Hi George,

Thank you for the response. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)

Nov 18 '05 #4
Hi George,

We are still researching this issue. We will post more information as soon
as we can.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
X-Tomcat-ID: 199496731
References: <Ox**************@TK2MSFTNGP09.phx.gbl> <Da**************@cpmsftngxa07.phx.gbl>
<u4**************@TK2MSFTNGP12.phx.gbl> MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: v-******@online.microsoft.com (Steven Cheng[MSFT])
Organization: Microsoft
Date: Wed, 04 Feb 2004 11:54:58 GMT
Subject: Re: strugling with Atributed XML parsing.
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
Message-ID: <ww**************@cpmsftngxa07.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Lines: 9
Path: cpmsftngxa07.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:207429
NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122

Hi George,

Thank you for the response. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)


Nov 18 '05 #5
I used the Xml Schema tool(xsd.exe) to automatically generate the schema
and serialize mapping class. To my surprise, the tool generated class
worked. Here is the xml format I used as example to generate class:
<clsOrder>
<MyHeader>Test Order</MyHeader>
<MyLine> dsfsdffd</MyLine>
<MyLine>dsfsdf</MyLine>
<MyLine> fdfdsf</MyLine>
</clsOrder>

Then, I used xsd.exe to generate the schema and class ,here is the
generated class:

[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)] public class clsOrder {
[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string MyHeader;
[System.Xml.Serialization.XmlElementAttribute("MyLi ne",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public clsOrderMyLine[] MyLine;
}

public class clsOrderMyLine {

[System.Xml.Serialization.XmlTextAttribute()]
public string Value;
}

[System.Xml.Serialization.XmlRootAttribute(Namespac e="",
IsNullable=false)] public class NewDataSet {

[System.Xml.Serialization.XmlElementAttribute("clsO rder")]
public clsOrder[] Items;
}

It could serialize and deserialize with the xml format the customer exactly
want. Hope this help!

-Thank you
Madhu
Microsoft Developer Engineer

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #6
closing this thread

Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
0
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
5
by: randy | last post by:
Can some point me to a good example of parsing XML using C# 2.0? Thanks
2
by: i am lost | last post by:
I have code that I am strugling to write could u plz help me with it ...I 'll be thankfull Implement a Priority Queue using Stacks. I am not allowed to use any other data structure. Example: ...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.