I have a datatable in asp.net 2.0 and C# as Code behind. My question is how can i convert this datatable into XML and save it to my project directory. And How can i call it back from its location into any dataset, datatable, and how can i query this XML..?
Give example with code please...
Thanks
Thanks & Best Regards,
-------------------------
M. Kashan Khan
My question is how can i convert this datatable into XML and save it to my project directory.
If you have the DataTable already you can use its WriteXml method to save the datatable into an Xml file.
The WriteXml method accepts a string argument that is the file location of where you want to save the file to.
Muhammad Kashan Khan
And How can i call it back from its location into any dataset, datatable, and how can i query this XML..?
To read the Xml back into the datatable, the datatable exposes a method called ReadXml. and it too accepts a string argument which is the file location of the Xml file.
You can query Xml file using the XML DOM by loading it into an XmlDocument. You can then use XPath to query the elements and nodes.
Marked as answer by jimmy q on Jul 03, 2009 03:51 AM
sure u can go all the way as Jimmy suggested.. there can be another way..of using serilization and deserilization..wud u like to do that> then i can explain furthure.
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Now to store this xml In ur Project Direcory u can do something like this.
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(strXml);
xDoc.Save(Server.MapPath("XML\\myFile.xml")); //XML is the folder name and myFile.xml is ur new file name
Muhammad Kashan Khan
How can i call it back from its location into any dataset, datatable
U can desirilize it and convert it back to object.
XmlNodeList xNodeList = xDoc.SelectNodes("DocumentElement/UserRights[@IsAllowed = 1]");
//The above statment will load all the UserRIghts node where is allowed = 1 in the xNodeList object.
string sect = string.Empty;
Label myLable = new Label();
foreach(XmlNode xNode in xNodeList)
{
foreach (XmlAttribute xAtt in xNode.Attributes)
{
if (xAtt.Name.ToLower() == "sectionid" && xAtt.Value == "5")
{
sect = xNode.Attributes["SectionTitle"].Value;
//break; Write break if u know ther is only one value
}
}
}
myLable.Text = sect;
Now in previous example i have explained to u how to load the xml in dataset and so on...
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Muhammad Kas...
Member
56 Points
104 Posts
How can i convert my datatable into XML using C# 2.0?
Jul 01, 2009 07:00 AM|LINK
Hi,
I have a datatable in asp.net 2.0 and C# as Code behind. My question is how can i convert this datatable into XML and save it to my project directory. And How can i call it back from its location into any dataset, datatable, and how can i query this XML..?
Give example with code please...
Thanks
-------------------------
M. Kashan Khan
jimmy q
All-Star
54108 Points
3578 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 01, 2009 08:20 AM|LINK
If you have the DataTable already you can use its WriteXml method to save the datatable into an Xml file.
The WriteXml method accepts a string argument that is the file location of where you want to save the file to.
To read the Xml back into the datatable, the datatable exposes a method called ReadXml. and it too accepts a string argument which is the file location of the Xml file.
You can query Xml file using the XML DOM by loading it into an XmlDocument. You can then use XPath to query the elements and nodes.
kavita_khand...
Star
9767 Points
1930 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 01, 2009 10:45 AM|LINK
sure u can go all the way as Jimmy suggested.. there can be another way..of using serilization and deserilization..wud u like to do that> then i can explain furthure.
I would love to change the world, but they wont give me the source code.
Muhammad Kas...
Member
56 Points
104 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 01, 2009 01:54 PM|LINK
Hi,
Yes kavita plz explain me if u can give me some information with code..
Thanks
-------------------------
M. Kashan Khan
kavita_khand...
Star
9767 Points
1930 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 02, 2009 05:09 AM|LINK
This is how u Convert ur DataTable in XMl(u can not directly serilize datatable..u have to fille it in Dataset)
DataTable dt = new DataTable();
dt.Columns.Add("roomCode");
dt.Columns.Add("roomNo");
dt.Columns.Add("hotelCode");
DataRow dr;
dr = dt.NewRow();
dr["roomCode"] = "1";
dr["roomNo"] = "MKavs";
dr["hotelCode"] = "222";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["roomCode"] = "2";
dr["roomNo"] = "232";
dr["hotelCode"] = "33";
dt.Rows.Add(dr);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
XmlElement xE = (XmlElement)Serialize(ds);
string strXml = xE.OuterXml.ToString();
Here the strXml will provide u the xml.
Now to store this xml In ur Project Direcory u can do something like this.
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(strXml);
xDoc.Save(Server.MapPath("XML\\myFile.xml")); //XML is the folder name and myFile.xml is ur new file name
U can desirilize it and convert it back to object.
DataSet newDs = (DataSet)Deserialize(xDoc.DocumentElement,typeof(DataSet));
No can u tell me that if u wan to query this xml after it is dserilized or when it is serilized?
also the Serilize and deserilize metrhods are are following.
/// <summary>
/// Serialize given object into XmlElement.
/// </summary>
/// <param name="transformObject">Input object for serialization.</param>
/// <returns>Returns serialized XmlElement.</returns>
#region Serialize given object into stream.
public XmlElement Serialize(object transformObject)
{
XmlElement serializedElement = null;
try
{
MemoryStream memStream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(transformObject.GetType());
serializer.Serialize(memStream, transformObject);
memStream.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(memStream);
serializedElement = xmlDoc.DocumentElement;
}
catch (Exception SerializeException)
{
}
return serializedElement;
}
#endregion // End - Serialize given object into stream.
/// <summary>
/// Deserialize given XmlElement into object.
/// </summary>
/// <param name="xmlElement">xmlElement to deserialize.</param>
/// <param name="tp">Type of resultant deserialized object.</param>
/// <returns>Returns deserialized object.</returns>
#region Deserialize given string into object.
public object Deserialize(XmlElement xmlElement, System.Type tp)
{
Object transformedObject = null;
try
{
Stream memStream = StringToStream(xmlElement.OuterXml);
XmlSerializer serializer = new XmlSerializer(tp);
transformedObject = serializer.Deserialize(memStream);
}
catch (Exception DeserializeException)
{
}
return transformedObject;
}
#endregion // End - Deserialize given string into object.
/// <summary>
/// Conversion from string to stream.
/// </summary>
/// <param name="str">Input string.</param>
/// <returns>Returns stream.</returns>
#region Conversion from string to stream.
public Stream StringToStream(String str)
{
MemoryStream memStream = null;
try
{
byte[] buffer = Encoding.UTF8.GetBytes(str);//new byte[str.Length];
memStream = new MemoryStream(buffer);
}
catch (Exception StringToStreamException)
{
}
finally
{
memStream.Position = 0;
}
return memStream;
}
#endregion // End - Conversion from string to stream.
I would love to change the world, but they wont give me the source code.
Muhammad Kas...
Member
56 Points
104 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 02, 2009 06:59 AM|LINK
Thanks Kavita for the Perfect solution.
One more thing i have an xml File :
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><?xml version="1.0" standalone="yes"?></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><DocumentElement></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <UserRights IsAllowed="1" SectionID="5" SectionTitle="Journal Voucher" ControlId="[View]" SectionOptionID="1" OptionTitle="View" ModuleID="6" ResourceKey="10000000" /></div><?xml version="1.0" standalone="yes"?>
<DocumentElement>
<UserRights IsAllowed="1" SectionID="5" SectionTitle="Journal Voucher" ControlId="[View]" SectionOptionID="1" OptionTitle="View" ModuleID="6" ResourceKey="10000000" />
</DocumentElement>
How can i query this xml file. as i want to get my required results for example
i want to get the result set when "isAllowed=1 and SectionID=5" I want to apply this filter.
and the returning result can be set as datasource for gridview or just to set as label.
Plz help with code.
Thanks again.
-------------------------
M. Kashan Khan
jimmy q
All-Star
54108 Points
3578 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 02, 2009 07:32 AM|LINK
You can use load the XML string fragment into the DOM using the XmlDocument.
Then you can use SelectSingleNode or any other method of the xmldocument and pass it an xpath query to query the Xml.
Alternatively you can use the XPathNavigator as well.
Have a read into XPath and how you can use it to query Xml documents/fragments
http://www.w3schools.com/XPath/default.asp
Muhammad Kas...
Member
56 Points
104 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 02, 2009 08:14 AM|LINK
Can u give me some example with some piece of code?
for my above requirement for filtering record.?
-------------------------
M. Kashan Khan
kavita_khand...
Star
9767 Points
1930 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 02, 2009 09:08 AM|LINK
Im glad that my answer was helpful to u.. pleaseMark It As Answer to all of the threard that was helpfult to u
also if u want to query ur xml..u can do like this.
suppose ur XML is like this
<DocumentElement>
<UserRights IsAllowed="1" SectionID="5" SectionTitle="Journal Voucher" ControlId="[View]" SectionOptionID="1" OptionTitle="View" ModuleID="6" ResourceKey="10000000"/>
<UserRights IsAllowed="0" SectionID="5" SectionTitle="Journal Voucher11" ControlId="[View]" SectionOptionID="1" OptionTitle="View" ModuleID="6" ResourceKey="10000000"/>
<UserRights IsAllowed="0" SectionID="9" SectionTitle="Journal Voucher22" ControlId="[View]" SectionOptionID="1" OptionTitle="View" ModuleID="6" ResourceKey="10000000"/>
</DocumentElement>
Then u can load it in xmlDom to query it
XmlNodeList xNodeList = xDoc.SelectNodes("DocumentElement/UserRights[@IsAllowed = 1]");
//The above statment will load all the UserRIghts node where is allowed = 1 in the xNodeList object.
string sect = string.Empty;
Label myLable = new Label();
foreach(XmlNode xNode in xNodeList)
{
foreach (XmlAttribute xAtt in xNode.Attributes)
{
if (xAtt.Name.ToLower() == "sectionid" && xAtt.Value == "5")
{
sect = xNode.Attributes["SectionTitle"].Value;
//break; Write break if u know ther is only one value
}
}
}
myLable.Text = sect;
Now in previous example i have explained to u how to load the xml in dataset and so on...
I would love to change the world, but they wont give me the source code.
Muhammad Kas...
Member
56 Points
104 Posts
Re: How can i convert my datatable into XML using C# 2.0?
Jul 02, 2009 09:19 AM|LINK
Really it helped me out...keep helping in future too. INSHA ALLAH...
and many many thanks to u...
-------------------------
M. Kashan Khan