-1

in my web application, i get result as xml when display on web browser like this

<Responses>
<Response0>
    <Action>sendMessage</Action>
    <Data>
        <AcceptReport>
            <StatusCode>0</StatusCode>
            <StatusText>Message accepted for delivery</StatusText>
            <MessageID>89c8011c-e291-44c3-ac72-cd35c76cb29d</MessageID>
            <Recipient>+85568922903</Recipient>
        </AcceptReport>
    </Data>
</Response0>
</Responses>

i want above XML file display as text like below:

Message accepted for delivery
Message ID: IEUHSHIL
Recipient: +441234567

how can i convert xml file to text above?

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
Toeur Tenh
  • 71
  • 4
  • 14
  • It's a little unclear what you mean - do you mean you have a web page on which you are writing string value and it's coming out like this? Or do you mean you are returning the XML to the browser and it's displaying the whole thing as XML (i.e. in IE you get the collapsible XML tree) - if the latter then you probably want to look at XSL – Andras Zoltan Aug 09 '12 at 07:40
  • i mean have a web page on which you are writing string value and it's coming out like this. so how can i do? – Toeur Tenh Aug 09 '12 at 07:42
  • Hi i updated my answer with full soruce ..just check it .. dont forget to upvote and mark answer as accepted if it works for you – Pranay Rana Aug 09 '12 at 08:14
  • @ToeurTenh Why did [you ask this question *again*](http://stackoverflow.com/questions/11879153/translate-xml-file-to-text)? Admittedly that version is better than this one - so you need to edit this one with the details of the other please. – Andras Zoltan Aug 09 '12 at 08:49
  • sorry becuase during i am editing this posting , when i click on save edition, it can not process. i don't know because of what that why i make one more question – Toeur Tenh Aug 09 '12 at 09:36

1 Answers1

3

EDIT

Source Code

XDocument doc = XDocument.Parse("<Responses> <Response0>     <Action>sendMessage</Action>     <Data>         <AcceptReport>             <StatusCode>0</StatusCode>             <StatusText>Message accepted for delivery</StatusText>             <MessageID>89c8011c-e291-44c3-ac72-cd35c76cb29d</MessageID>             <Recipient>+85568922903</Recipient>         </AcceptReport>     </Data> </Response0> </Responses> ");

var message = from item in doc.Descendants("AcceptReport")
               select new { 
                    StatusText = item.Element("StatusText").Value,
                    MessageID = item.Element("MessageID").Value,
                    Recipient = item.Element("Recipient").Value 
               };
foreach (var el in message)
{
    Console.WriteLine(el.MessageID + " " +el.Recipient + " " + el.StatusText);
}

Note : make use of XDocument.Parse if you are passing string instead of xml file.


Easy way to do it make use of Linq To XML.

Check over there : Reading XML documents using LINQ to XML

not sure but something like this

var message=  
              from item in XElement.Load("message.xml").Descendants("Data") 
                 select new
                 {
                      StatusText= item.Element("StatusText").Value,
                      MessageID= item.Element("MessageID").Value,
                      Recipient= item.Element("Recipient").Value
                  };
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263