- Populate the DataTable values from Storeproc
- Serialize
- Write to XMl
- Close the writing File
Enter Values to XML
- Create a XmlElement as Root
- Create a XmlElement below root
- Create XmlText which will be the value for XmlElement
- Append Child
- Save the File
Get the Element Value from XML
- Read the XML
- Deserilize the object so XML values are now populated to the DataTable
- Close the Read
- Instantiate xmlDoc
- Load it with the XML file
- Create a Node
- Goto the Node by using GetElementsByTagName("val1")[0];
- Get the values of that node by xNode.InnerText
#region Create XML
//Uncomment following lines to create XML
int val1 =1; // Initialize to an appropriate value
expected = className.FuncReturnsStoreProc(val1);
////Serialization
XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(expected.GetType());
TextWriter tw=new StreamWriter(strFilename);
xs.Serialize(tw, expected);
tw.Close();
////Create Input values to the XML Element
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFilename);
if (File.Exists(strFilename))
{
XmlElement elmRoot = xmlDoc.DocumentElement;
elmRoot = xmlDoc.DocumentElement;
XmlElement elmVal = xmlDoc.CreateElement("val1");
XmlText valToXML = xmlDoc.CreateTextNode(val1.ToString());
elmRoot.AppendChild(elmVal);
elmRoot.LastChild.AppendChild(valToXML);
xmlDoc.Save(strFilename);
}
//Uncomment ends
#endregion
#region Get XML data
//Deserialization
XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(expected.GetType());
TextReader tr = new StreamReader(strFilename);
expected = (ObjectName)xs.Deserialize(tr); //Convert to the ObjectName
tr.Close();
//////Reads Input parameter and pass it to the actual
int val1; // Gets the Input parameter value from XML
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFilename);
XmlNode xNode;
xNode = xmlDoc.GetElementsByTagName("val1")[0];
val1 = Convert.ToInt32(xNode.InnerText);
#endregion
Thanks :http://www.functionx.com/csharp/xml/Lesson03.htm
No comments:
Post a Comment