Results 1 to 10 of 10

Thread: How to read XML document and convert into a string in C#

  1. #1
    Join Date
    Feb 2009
    Posts
    64

    How to read XML document and convert into a string in 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# ?

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: How to read XML document and convert into a string in 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();
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to read XML document and convert into a string in C#

    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.

  4. #4
    Join Date
    Feb 2009
    Posts
    40

    Re: How to read XML document and convert into a string in C#

    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..

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to read XML document and convert into a string in C#

    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 !

  6. #6
    Join Date
    Feb 2010
    Posts
    1

    question Re: How to read XML document and convert into a string in C#

    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.

  7. #7
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to read XML document and convert into a string in C#

    Quote Originally Posted by jorandpg View Post
    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.
    Your query is incomplete. You didn't provided the code on which you are getting the above output. It is always advised to give the complete information about your problem to get the adequate solution.

  8. #8
    Join Date
    May 2009
    Posts
    543

    Re: How to read XML document and convert into a string in C#

    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.

  9. #9
    Join Date
    Mar 2010
    Posts
    145

    Re: How to read XML document and convert into a string in C#

    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.

  10. #10
    Join Date
    May 2012
    Posts
    7

    Re: How to read XML document and convert into a string in C#

    you can create this with the XML Sitemap in the C# with the required functions in the CSS.

Similar Threads

  1. How to convert HP Jetsuite Document to PDF Document
    By Li-One in forum Hardware Peripherals
    Replies: 4
    Last Post: 01-02-2012, 02:09 PM
  2. Convert string into int in C
    By screwball in forum Software Development
    Replies: 4
    Last Post: 22-12-2011, 08:47 PM
  3. Convert XML string into DOM
    By GreatThinker in forum Software Development
    Replies: 6
    Last Post: 22-07-2010, 09:48 AM
  4. How to read string from a given text file using C# program?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 10:18 PM
  5. How to convert string to int
    By Zavier in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 06:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,462,469.69837 seconds with 17 queries