I want to read an XML document using C#. Can anybody please provide me the information about how to load an XML document using C#. Also, I need to convert the same XML doument into the string. How can I do this using C# ?
I want to read an XML document using C#. Can anybody please provide me the information about how to load an XML document using C#. Also, I need to convert the same XML doument into the string. How can I do this using C# ?
Here's the simple utility on how to load an XML file into XmlDocument object and then load it into a String object. This seems to be really easy because when you call ToString method on XmlDocument object, it does not return you its contents but it returns the full class name System.Xml.XmlDocument.
Code:static string GetXmlString(string strFile) { // Load the xml file into XmlDocument object. XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(strFile); } catch (XmlException e) { Console.WriteLine(e.Message); } // Now create StringWriter object to get data from xml document. StringWriter sw = new StringWriter(); XmlTextWriter xw = new XmlTextWriter(sw); xmlDoc.WriteTo(xw); return sw.ToString(); }
Click Here - Read and Write an XML document in C#
This is a very useful article which will explain you about how to read(or load) and write the XML document in C#. It gives all the necessary details along with the sample examples to understand the concepts quite easily and clearly.
When I tried to load an xml document like the following :
private void Display()
{
try
{
XmlDocument doc = new XmlDocument();
string url = @"D:\NewFolder\Notebook\Notebook\Xml\diary.xml";
doc.Load(url);
DateTime t = dateTimePicker1.Value;
string date = Convert.ToString(t.Day) + "/" + Convert.ToString(t.Month) + "/" + Convert.ToString(t.Year);
XmlNode node = doc.SelectSingleNode("diary[@date = " + date + "]");
textBox1.Text = node.Value;
}
catch (System.IO.FileNotFoundException e)
{
MessageBox.Show(e.Message);
}
I received the following exceptions -
- A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
- A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
- A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll
- A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in System.Xml.dll
Well, I presumed that the emulator doesn't know the path (url) on PC, but I am not aware on how to correct the same..
Try mounting the folder that contains your XML document as a flash memory device on the emulator (if your emulator allows) and change the path to match it. I am not sure whether it works for Pocket PC 2003 emulator, but it works on a WM 5 emulator...thats for sure !
hi.
i have a problem with is code en pocketPC
the output is IOException and .. i don't know what is the problem, so, the code is equal.
For that I will recommend you to refer XML data binding. Go through some detailed documents on web which will guide you to work with XML on C platform. With a typical C++ app it is possible to manipulate the data of XML file. But you need to ensure that there should be common api access used. Here there are two types of common apps used which is DOM and SAX. This cannot be explained in some lines. There are nice documentation done on which detailed example scripts.
Have a look on CodeSynthesis XSD. This is a open source platfrom for c++ data binding. It provide you XML suport and helps to generate the c# programing classes . Here you can acces the data in XML format. CodeSynthesis XSD generate codes on the basis of xml parsing. It is a cross platform support application that can help you to work with xml files easily. Try to read on this more before using the same.
you can create this with the XML Sitemap in the C# with the required functions in the CSS.
Bookmarks