Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Mon, 31 Mar 2008 04:40:01 -0700,    group: microsoft.public.xml        back       


simple xml de-serializer test   
Hi,

I am trying to read my xml file and output in console prompt.
I get the error message: "There is an error in XML document (2, 2)."
I'm not very sure what this message means. Any advice would be appreciated.
cheers
Andrew

code
====
static void Main(string[] args)
 {
  clsProduct p  = new clsProduct();
   XmlSerializer reader = new XmlSerializer(p.GetType());
   System.IO.StreamReader file = new 
System.IO.StreamReader(@"C:\Output\Product.xml");
   p = (clsProduct) reader.Deserialize(file); <-- ERROR LINE

   Console.Write(
    p.ItemName + "\t" +
    p.Description + "\t" +
    Convert.ToString(p.UnitPrice) + "\t" +
    Convert.ToString(p.Qty) + "\t" +
    Convert.ToString(p.LineTotal));
    Console.ReadLine();
 }

Product.xml
========
<?xml version="1.0" encoding="utf-8"?>
<OrderedItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ItemName>Widget</ItemName>
  <Description>Faster, better, cheaper</Description>
  <UnitPrice>5</UnitPrice>
  <Quantity>4</Quantity>
  <LineTotal>10</LineTotal>
</OrderedItem>

clsProduct.cs
=========
    public class clsProduct
    {
        public string ItemName
        {
            get { return ItemName; }
            set { ItemName = value; }
        }

        public string Description
        {
            get { return Description; }
            set { Description = value; }
        }
        
        public decimal UnitPrice
        {
            get { return UnitPrice; }
            set { UnitPrice = value; }
        }

        [XmlElement(ElementName="Quantity")]
        public int Qty
        {
            get { return Qty; }
            set { Qty = value; }
        }

        public decimal LineTotal
        {
            get { return LineTotal; }
            set { LineTotal = value; }
        }
    }
date: Mon, 31 Mar 2008 04:40:01 -0700   author:   Andrew

Re: simple xml de-serializer test   
Andrew wrote:

> I am trying to read my xml file and output in console prompt.
> I get the error message: "There is an error in XML document (2, 2)."
> I'm not very sure what this message means. Any advice would be appreciated.

Well the serializer looks for an element of the class name, unless you 
specify it differently
> <OrderedItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
>   <ItemName>Widget</ItemName>
>   <Description>Faster, better, cheaper</Description>
>   <UnitPrice>5</UnitPrice>
>   <Quantity>4</Quantity>
>   <LineTotal>10</LineTotal>
> </OrderedItem>
> 
> clsProduct.cs
> =========
     [XmlRoot(ElementName = "OrderedItem")]
>     public class clsProduct
>     {
>         public string ItemName
>         {
>             get { return ItemName; }
>             set { ItemName = value; }
>         }

However you have more serious problems in all your properties, they will 
lead to a stack overflow as they are recursive.
So replace that code with e.g.
           private string itemName;
           public string ItemName
           {
               get { return itemName; }
               set { itemName = value; }
           }
and do the same for the other properties.




-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Mon, 31 Mar 2008 14:48:02 +0200   author:   Martin Honnen

Re: simple xml de-serializer test   
Thanks it works beautifully.

I modified the code to deserialize a larger xml file which goes something 
like this:
Is the code the same to de-serialize ?

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetResultResponse xmlns="http://Product/Books/Store/V2">
      <InvestigationReport>
        <ReportData xmlns="http://Product/Books/Store">
          <ServiceProvider />
          <RequestingParty>
            <HcpId xmlns="http://Product/Books/General">
              <IdValue>ABC001</IdValue>
              <IdScheme>Requestor</IdScheme>
              <IdType>Administrator</IdType>
            </HcpId>
        </DocumentData>
      </InvestigationReport>
    </GetResultResponse>
  </soap:Body>
</soap:Envelope>

Thanks
Andrew

"Martin Honnen" wrote:

> Andrew wrote:
> 
> > I am trying to read my xml file and output in console prompt.
> > I get the error message: "There is an error in XML document (2, 2)."
> > I'm not very sure what this message means. Any advice would be appreciated.
> 
> Well the serializer looks for an element of the class name, unless you 
> specify it differently
> > <OrderedItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> >   <ItemName>Widget</ItemName>
> >   <Description>Faster, better, cheaper</Description>
> >   <UnitPrice>5</UnitPrice>
> >   <Quantity>4</Quantity>
> >   <LineTotal>10</LineTotal>
> > </OrderedItem>
> > 
> > clsProduct.cs
> > =========
>      [XmlRoot(ElementName = "OrderedItem")]
> >     public class clsProduct
> >     {
> >         public string ItemName
> >         {
> >             get { return ItemName; }
> >             set { ItemName = value; }
> >         }
> 
> However you have more serious problems in all your properties, they will 
> lead to a stack overflow as they are recursive.
> So replace that code with e.g.
>            private string itemName;
>            public string ItemName
>            {
>                get { return itemName; }
>                set { itemName = value; }
>            }
> and do the same for the other properties.
> 
> 
> 
> 
> -- 
> 
> 	Martin Honnen --- MVP XML
> 	http://JavaScript.FAQTs.com/
>
date: Mon, 31 Mar 2008 06:18:00 -0700   author:   Andrew

Re: simple xml de-serializer test   
Andrew wrote:

> I modified the code to deserialize a larger xml file which goes something 
> like this:
> Is the code the same to de-serialize ?
> 
> <?xml version="1.0" encoding="utf-8"?>
> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>   <soap:Body>
>     <GetResultResponse xmlns="http://Product/Books/Store/V2">
>       <InvestigationReport>
>         <ReportData xmlns="http://Product/Books/Store">
>           <ServiceProvider />
>           <RequestingParty>
>             <HcpId xmlns="http://Product/Books/General">
>               <IdValue>ABC001</IdValue>
>               <IdScheme>Requestor</IdScheme>
>               <IdType>Administrator</IdType>
>             </HcpId>
>         </DocumentData>
>       </InvestigationReport>
>     </GetResultResponse>
>   </soap:Body>
> </soap:Envelope>

That is SOAP so you might want to look into SOAP serialization 
<URL:http://msdn2.microsoft.com/en-us/library/564k8ys4(VS.80).aspx>


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Mon, 31 Mar 2008 15:57:06 +0200   author:   Martin Honnen

Re: simple xml de-serializer test   
Hi,

Thanks for your reply. From what I've read, the code is different for SOAP 
Serialization, but for de-serialization, it's essentially the same. Am I 
right ?

What I need to figure out is which is the SoapType, XMLRoot, XmlType, etc 
tags...

cheers
Andrew

"Martin Honnen" wrote:

> Andrew wrote:
> 
> > I modified the code to deserialize a larger xml file which goes something 
> > like this:
> > Is the code the same to de-serialize ?
> > 
> > <?xml version="1.0" encoding="utf-8"?>
> > <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> > xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> >   <soap:Body>
> >     <GetResultResponse xmlns="http://Product/Books/Store/V2">
> >       <InvestigationReport>
> >         <ReportData xmlns="http://Product/Books/Store">
> >           <ServiceProvider />
> >           <RequestingParty>
> >             <HcpId xmlns="http://Product/Books/General">
> >               <IdValue>ABC001</IdValue>
> >               <IdScheme>Requestor</IdScheme>
> >               <IdType>Administrator</IdType>
> >             </HcpId>
> >         </DocumentData>
> >       </InvestigationReport>
> >     </GetResultResponse>
> >   </soap:Body>
> > </soap:Envelope>
> 
> That is SOAP so you might want to look into SOAP serialization 
> <URL:http://msdn2.microsoft.com/en-us/library/564k8ys4(VS.80).aspx>
> 
> 
> -- 
> 
> 	Martin Honnen --- MVP XML
> 	http://JavaScript.FAQTs.com/
>
date: Mon, 31 Mar 2008 08:38:00 -0700   author:   Andrew

Re: simple xml de-serializer test   
Hi,
I got a parse error "Parse Error, no assembly associated with Xml key _P1 
GetResultResponse" with this code:

 using System.Xml;
 using System.Xml.Serialization;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Soap;

 IFormatter formatter;
 FileStream fileStream = null;
 Object objectFromSoap = null;
  try
  {
    string filePath = @"C:\FileStore\Result.xml";            
    fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    formatter = new SoapFormatter();
    objectFromSoap = formatter.Deserialize(fileStream); <-- error

    Console.Write(objectFromSoap.ToString());
    Console.ReadLine();
  }

Any advice ?

cheers
Andrew

"Andrew" wrote:

> Hi,
> 
> Thanks for your reply. From what I've read, the code is different for SOAP 
> Serialization, but for de-serialization, it's essentially the same. Am I 
> right ?
> 
> What I need to figure out is which is the SoapType, XMLRoot, XmlType, etc 
> tags...
> 
> cheers
> Andrew
> 
> "Martin Honnen" wrote:
> 
> > Andrew wrote:
> > 
> > > I modified the code to deserialize a larger xml file which goes something 
> > > like this:
> > > Is the code the same to de-serialize ?
> > > 
> > > <?xml version="1.0" encoding="utf-8"?>
> > > <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
> > > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> > > xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> > >   <soap:Body>
> > >     <GetResultResponse xmlns="http://Product/Books/Store/V2">
> > >       <InvestigationReport>
> > >         <ReportData xmlns="http://Product/Books/Store">
> > >           <ServiceProvider />
> > >           <RequestingParty>
> > >             <HcpId xmlns="http://Product/Books/General">
> > >               <IdValue>ABC001</IdValue>
> > >               <IdScheme>Requestor</IdScheme>
> > >               <IdType>Administrator</IdType>
> > >             </HcpId>
> > >         </DocumentData>
> > >       </InvestigationReport>
> > >     </GetResultResponse>
> > >   </soap:Body>
> > > </soap:Envelope>
> > 
> > That is SOAP so you might want to look into SOAP serialization 
> > <URL:http://msdn2.microsoft.com/en-us/library/564k8ys4(VS.80).aspx>
> > 
> > 
> > -- 
> > 
> > 	Martin Honnen --- MVP XML
> > 	http://JavaScript.FAQTs.com/
> >
date: Tue, 1 Apr 2008 01:58:01 -0700   author:   Andrew

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us