Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Saturday, August 8, 2009

How to transform between object and xml in c#

Hi,
Many times we need 2 make transforms object to xml and transform xml to object.
there is a very simple way in XmlSerializer class, we just have to call serialize or Deserialize function and we can get what want. I'll explain it by example:


let's say that we have RootData class:

public class RootData
{
public HeaderElement Header;
public List SubjectsList;
}
public class HeaderElement
{
public int DataType;
}
public class Subject
{
public string SubjectName;
public int SubjectID;
public int SubjectType;
}

and we need to make transforms with this xml:

<?xml version="1.0" encoding="utf-8"?>
<RootData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Header>
<DataType>1</DataType>
</Header>
<SubjectsList>
<Subject>
<SubjectName>Name1</SubjectName>
<SubjectID>1</SubjectID>
<SubjectType>1</SubjectType>
</Subject>
<Subject>
<SubjectName>Name2</SubjectName>
<SubjectID>2</SubjectID>
<SubjectType>2</SubjectType>
</Subject>
<Subject>
<SubjectName>Name3</SubjectName>
<SubjectID>3</SubjectID>
<SubjectType>3</SubjectType>
</Subject>
</SubjectsList>
</RootData>


The function 2 make transform from object to xml is:

public string SerializeObject(Type oType, object oRootData)
{
//@@@ Create xml from the object
MemoryStream oMemoryStream = null;
XmlTextWriter oXmlTextWriter = null;
string sXml = null;
try
{
oMemoryStream = new MemoryStream();
XmlSerializer oXmlSerializer = new XmlSerializer(oType);
oXmlTextWriter = new XmlTextWriter(oMemoryStream, Encoding.UTF8);
oXmlSerializer.Serialize(oXmlTextWriter, oRootData);
oMemoryStream = (MemoryStream)oXmlTextWriter.BaseStream;
UTF8Encoding oUTF8Encoding = new UTF8Encoding();
sXml = oUTF8Encoding.GetString(oMemoryStream.ToArray());
}
catch (Exception)
{
throw;
}
finally
{
if (oMemoryStream != null)
oMemoryStream.Close();
if (oXmlTextWriter != null)
oXmlTextWriter.Close();
}
return sXml;
}

The function 2 make transform from xml to object is:

public object DeserializeObject(Type oType, string sXml)
{
//@@@ Create object from the xml
MemoryStream oMemoryStream = null;
object oRootData = null;
try
{
XmlSerializer oXmlSerializer = new XmlSerializer(oType);
UTF8Encoding oUTF8Encoding = new UTF8Encoding();
Byte[] Bytes = oUTF8Encoding.GetBytes(sXml);
oMemoryStream = new MemoryStream(Bytes);
oRootData = oXmlSerializer.Deserialize(oMemoryStream);
}
catch (Exception)
{
throw;
}
finally
{
if (oMemoryStream != null)
oMemoryStream.Close();
}
return oRootData;
}

Before u run this example, u can fill your object with this function:

public RootData CreateData()
{
RootData oRootData = new RootData();
HeaderElement oHeaderElement = new HeaderElement();
oHeaderElement.DataType = 1;
oRootData.Header = oHeaderElement;

Subject oSubject1 = new Subject();
oSubject1.SubjectID = 1;
oSubject1.SubjectName = "Name1";
oSubject1.SubjectType = 1;

Subject oSubject2 = new Subject();
oSubject2.SubjectID = 2;
oSubject2.SubjectName = "Name2";
oSubject2.SubjectType = 2;

Subject oSubject3 = new Subject();
oSubject3.SubjectID = 3;
oSubject3.SubjectName = "Name3";
oSubject3.SubjectType = 3;

List Subjects = new List();
Subjects.Add(oSubject1);
Subjects.Add(oSubject2);
Subjects.Add(oSubject3);

oRootData.SubjectsList = Subjects;
return oRootData;
}

And run this code to execute the transform:

RootData oRootData = CreateData();
string sXml = SerializeObject(typeof(RootData), oRootData);
oRootData = (RootData)DeserializeObject(typeof(RootData), sXml);


In this case when every data member trnsform 2 xml node we dont have to change or add nothing in the class, but there is a few useful attributes that we can use (just add it above the data member class):

  • [XmlElement("<Xml Node Name>")] - by default, the XmlSerializer uses the data member name as the xml node name, if u want 2 change it - use this attribute

  • [XmlElement(("<Xml Node Name>"), IsNullable = false)] - says that if the data member value is null it will not appear in the transformed xml

  • [XmlAttribute] - use it when u want that the data member output will be xml attribute and not xml node

Sunday, May 24, 2009

Simple XPath Examples

Hi,
In this post i'll show some simple(but useful) XPath Examples
let's say that i have this xml file named 'Persons.xml':


<?xml version='1.0'?>

<Persons>

<Person Height="180">

<FullName>Yosi Havia</FullName>

<Age>18</Age>

</Person>

<Person Height="177">

<FullName>Yosi Cohen</FullName>

<Age>22</Age>

</Person>

<Person Height="169">

<FullName>Itay Cohen</FullName>

<Age>32</Age>

</Person>

</Persons>


so, let's talk code lines:


//@@@ Select all the persons in 18 age
XmlNodeList oXmlNodeList =
XmlPersons.SelectNodes("/Persons/Person[Age = '" + 18 + "']");


//@@@ Select all the persons with age greater than 17
XmlNodeList oXmlNodeList =
XmlPersons.SelectNodes("/Persons/Person[Age > '" + 17 + "']");


//@@@ Select all the persons that contains Yosi in their names
XmlNodeList oXmlNodeList =
XmlPersons.SelectNodes("/Persons/Person/FullName[contains(.,'" + "Yosi" + "')]");


//@@@ Select all the persons with 180 height(attribute)
XmlNodeList oXmlNodeList =
XmlPersons.SelectNodes("/Persons/Person[@Height = '" + "180" + "']");

Saturday, May 9, 2009

How to merge 2 XmlDocuments

Hi,
In this post i'll show u how 2 merge 2 xml documents
let's say that i have 2 xml documents,

Persons.xml

<?xml version='1.0'?>

<Persons>

<Person Height="180">

<FullName>Yosi Havia</FullName>

<Age>18</Age>

</Person>

<Person Height="177">

<FullName>Yosi Cohen</FullName>

<Age>22</Age>

</Person>

<Person Height="169">

<FullName>Itay Cohen</FullName>

<Age>32</Age>

</Person>

</Persons>



and Persons2.xml

<?xml version='1.0'?>

<Persons>

<Person Height="175">

<FullName>Yosi Havia</FullName>

<Age>15</Age>

</Person>

</Persons>



What i want 2 do in this example is 2 take the node with the full name 'Yosi Havia' from Persons2.xml file and replace the original node with the same full name in Persons.xml file.
this code lines make this assigment(with help of my last post Simple XPath Examples ):

XmlDocument XmlPersons1 = new XmlDocument();
XmlPersons1.Load(@"Persons.xml");

XmlDocument XmlPersons2 = new XmlDocument();
XmlPersons2.Load(@"Persons2.xml");

XmlNode oXmlNewData = XmlPersons2.SelectSingleNode("/Persons/Person[FullName = '" + "Yosi Havia" + "']");
XmlNode targetNode = XmlPersons1.SelectSingleNode("/Persons/Person[FullName = '" + "Yosi Havia" + "']");
//@@@ Add the node from XmlPersons2.xml to XmlPersons.xml
XmlNode sourceNode = XmlPersons1.ImportNode(oXmlNewData, true);
//@@@ Replace the original node
XmlPersons1.DocumentElement.ReplaceChild(sourceNode, targetNode);

and u can c the result here:

<?xml version="1.0"?>

<Persons>

<Person Height="175">

<FullName>Yosi Havia</FullName>

<Age>15</Age>

</Person>

<Person Height="177">

<FullName>Yosi Cohen</FullName>

<Age>22</Age>

</Person>

<Person Height="169">

<FullName>Itay Cohen</FullName>

<Age>32</Age>

</Person>

</Persons>