strugling with Atributed XML parsing.

  • Thread starter Thread starter George Ter-Saakov
  • Start date Start date
G

George Ter-Saakov

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.
 
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 = 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-us/cpguide/html/cpconintroducingxmlseri
alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconanexampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrollingseriali
zationbyxmlserializerwithattributes.asp?frame=true

#Attributes That Control XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconattributesthatcont
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.)
 
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 said:
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 = 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-us/cpguide/html/cpconintroducingxmlseri
alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconanexampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrollingseriali
zationbyxmlserializerwithattributes.asp?frame=true

#Attributes That Control XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconattributesthatcont
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.)
 
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.)
 
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: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: (e-mail address removed) (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: <[email protected]>
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.)
 
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(Namespace="",
IsNullable=false)] public class clsOrder {


[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string MyHeader;


[System.Xml.Serialization.XmlElementAttribute("MyLine",
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(Namespace="",
IsNullable=false)] public class NewDataSet {

[System.Xml.Serialization.XmlElementAttribute("clsOrder")]
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.)
 
Back
Top