none
how to convert xml file into a xml string

    Question


  • How can I convert a XML file into a xml string.

    I have tried streamreader, xmltextreader, the out put results only give me the text() part of xml file(all xml tags are not included).

    In other words How can I convert xml file into
    A string =
    "<?xml version="1.0"?><Registration
    xmlns="NZL:govt:IRD:KiwiSaver:B2BInterfaces:RegisterMembe r:v1.0"
    xmlns:xAL="urnasis:names:tc:ciq:xal:3"
    xmlns:xNL="urnasis:names:tc:ciq:xnl:3
    </Registration>"

    Thank you very much.
    Monday, April 02, 2007 5:32 AM

Answers

  • Why you don't load it to XmlDocument and write it back to string several more times?

    This should work as well.

     

    Seriously:

    Ying, StreamReader can't strip markup. It doesn't know anything about XML.

    In your sample not clear what document2 is. May be it doesn't have tags already.

    How you know that lblxml.text don't have tags? If lblxml is HTML viewer it would not show you XML tags. It doesn't know how to do this.

     

    Simple proof:

    Dim rd As TextReader
            rd = New StreamReader("test.xml")

    Dim str as string
            str = rd.ReadToEnd

    Console.WriteLine(str);

     

    Open test.xml in notepad to be sure that XML has tags.

    Run code above from command prompt. You should see the same XML. This means that 'str' has all your tags.

     

    Good luck.

    Tuesday, April 03, 2007 8:12 AM

All replies

  • hi

    you can use the xmldocument class load method, and then use the innerxml, outerxml properties.
    (http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument.innerxml.aspx)

    or you use your xmltextreader, put the reader on the first node, and then use the innerxml,outerxml,...
    (http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.innerxml.aspx)

     

    Monday, April 02, 2007 5:49 AM
  • In another words you want read content of the file to a string.

    StreamReader is exectly what you need. ReadToEnd() would give you a string. 

    Monday, April 02, 2007 6:05 AM
  • Thank you for your reply.

    I have tried

    Dim xmldoc As XmlDocument = New XmlDocument()
            'doc.Load(document2)
            xmldoc.LoadXml("<root>" & _
                    "<elem>some text<child/>more text</elem>" & _
                    "</root>")
            Dim elem As XmlNode = xmldoc.DocumentElement.FirstChild
            xml &= elem.InnerXml
            Response.Write(xml)
            readXMLAsString = xml

    The output are:

    "some textmore text"

    Is there any way to display the whole thing?

     "<root>" & _
                    "<elem>some text<child/>more text</elem>" & _
                    "</root>"
    Monday, April 02, 2007 10:20 PM
  • Thank you for your reply

    I have also tried:

    Dim rd As TextReader
            rd = New StreamReader(document2)
            lblxml.text = rd.ReadToEnd

    The output is only the text part, without markup parts.

    I am not sure if there are something wrong with my code.
    Monday, April 02, 2007 10:24 PM

  • I just like to read a xml file to a string, the string must include markups and text.

    I have tried streamreader, xmldocument, xmlreader, but they only read the text part of xml.

    E.g. for a simple xml file:

    <Root>
       <book>title1</book>
    </Root>

    The output is only "title1", but I want the whole thing.

    The problem is how can I read  (more complicated than above) entire xml file into a string?

    Thank you.
    Monday, April 02, 2007 10:45 PM
  • you have to do it like this:

     

    Dim xmldoc As XmlDocument = New XmlDocument()
            'doc.Load(document2)
            xmldoc.LoadXml("<root>" & _
                    "<elem>some text<child/>more text</elem>" & _
                    "</root>")
            xml &= xmldoc.DocumentElement.OuterXml

          Response.Write(xml)
            readXMLAsString = xml

     

    innerxml will return all the markup of just the children of your current node. 

     

    outerxml will return the markup representing this node and all its child nodes

     

    this should work

     

    Tuesday, April 03, 2007 5:37 AM
  • Why you don't load it to XmlDocument and write it back to string several more times?

    This should work as well.

     

    Seriously:

    Ying, StreamReader can't strip markup. It doesn't know anything about XML.

    In your sample not clear what document2 is. May be it doesn't have tags already.

    How you know that lblxml.text don't have tags? If lblxml is HTML viewer it would not show you XML tags. It doesn't know how to do this.

     

    Simple proof:

    Dim rd As TextReader
            rd = New StreamReader("test.xml")

    Dim str as string
            str = rd.ReadToEnd

    Console.WriteLine(str);

     

    Open test.xml in notepad to be sure that XML has tags.

    Run code above from command prompt. You should see the same XML. This means that 'str' has all your tags.

     

    Good luck.

    Tuesday, April 03, 2007 8:12 AM