How do we create a myclass.cs based on a myxml.xml file. I try this using XMLSerializer with XMLReader and Memory stream but getting one error all the time. 'There is an error in XML document (1, 2).'
//Example:1
XmlReader xr = XmlReader.Create(new StringReader(@"C:\SOtest.xml"));
XmlTextReader xtr = new XmlTextReader(new StringReader(@"C:\SOtest.xml"));
XmlSerializer serializer2 = new XmlSerializer(typeof(SWIFTWebReference.ApplSpcfc));
SWIFTWebReference.ApplSpcfc resultingMessage = (SWIFTWebReference.ApplSpcfc)serializer2.Deserialize(xtr); //There is an error in XML document (1, 2).
//Example:2
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SWIFTWebReference.ApplSpcfc));
FileStream inStream = File.OpenRead(@"C:\SOtest.xml");
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(inStream.Length);
inStream.Read(storeStream.GetBuffer(), 0, (int)inStream.Length);
storeStream.Position = 0;
ap = (SWIFTWebReference.ApplSpcfc)xmlSerializer.Deserialize(storeStream); //There is an error in XML document (1, 2).
It may be that your class does not correctly match your XML. i.e. it's trying to deserialize the xml into a class that doesn't correctly match the XML schema.
This is really helpful, where you take your schema and then following the instruction on their site, it generates the cs class from the schema. It's super simple.
If you don't have a schema file (.xsd) of your xml, the simply open it in visual studio go to the XML menu on top -> create schema and voila, you have a schema. Save it and us it as Microsoft describes.
Good luck
--- Grace ties up all the loose ends and suddenly everything makes sense ---
tahazubairah...
Participant
887 Points
798 Posts
There is an error in XML document (1, 2)
Dec 04, 2012 08:19 AM|LINK
Hi,
How do we create a myclass.cs based on a myxml.xml file. I try this using XMLSerializer with XMLReader and Memory stream but getting one error all the time. 'There is an error in XML document (1, 2).'
Taha Zubair Ahmed
http://www.tahazubair.blogspot.com
dev_2580
Member
456 Points
79 Posts
Re: There is an error in XML document (1, 2)
Dec 04, 2012 10:59 AM|LINK
It may be that your class does not correctly match your XML. i.e. it's trying to deserialize the xml into a class that doesn't correctly match the XML schema.
To make sure that your class is extablished correctly for your XML file, I like to use a tool from Microsoft xsd.exe, heres the like: http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.71).aspx
This is really helpful, where you take your schema and then following the instruction on their site, it generates the cs class from the schema. It's super simple.
If you don't have a schema file (.xsd) of your xml, the simply open it in visual studio go to the XML menu on top -> create schema and voila, you have a schema. Save it and us it as Microsoft describes.
--- Grace ties up all the loose ends and suddenly everything makes sense ---
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: There is an error in XML document (1, 2)
Dec 05, 2012 01:17 AM|LINK
Hello,
Your xml file content doesn't match your existing model classes. So please have this sample: