Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Wednesday, November 16, 2011

xml DeSerialization xsd.exe

good article
-------

http://blogs.msdn.com/b/yojoshi/archive/2011/05/14/xml-serialization-and-deserialization-entity-classes-with-xsd-exe.aspx

below is simple steps using VS 2010

1) open XML file that you want to deserialize in VS 2010
2) from VS 2010 menu > XML > Create Schema
3) It will create schema in you project save somewhere this .xsd file (in project)
4) go to VS command prompt
5) change path to your .xsd file (where you save above .xsd file)
6) type : xsd myschemafile.xsd /c (must you /c)
7) it will generate myschemafile.cs file in same location
8) type following code

using (FileStream xmlStream = new FileStream(@"C:\book.xml", FileMode.Open))
{
using (XmlReader xmlReader = XmlReader.Create(xmlStream))
{
XmlSerializer serializer = new XmlSerializer(typeof(catalog));
catalog deserializedStudents = serializer.Deserialize(xmlReader) as catalog;
foreach (var b in deserializedStudents.book )
{
Console.WriteLine("author : {0}", b.author);
Console.WriteLine("description : {0}", b.description);
Console.WriteLine("genre : {0}", b.genre);
Console.WriteLine("price : {0}", b.price);
Console.WriteLine("");
}
}
}



Friday, September 11, 2009

The ' ' character, hexadecimal value 0x20, cannot be included in a name

While creating XElement or XMLElement from C# code,
probably you will get below error

Error:
The ' ' character, hexadecimal value 0x20, cannot be included in a name.

Solution:
XElement/attribute does not contain space between node name, make sure your node name doesn't have white spaces.

for example

<Row Total Cost="" />

change to
<Row TotalCost="" />


if you notice second one there is no white space on Attribute TotalCost


hope this helps

Monday, August 10, 2009