?
Solved

how to parse xml attributes to JSON

Posted on 2014-02-11
4
Medium Priority
?
607 Views
Last Modified: 2014-02-12
How do I parse xml attribute to JSON attributes?

StringBuilder output = new StringBuilder();
                String xmlString = response.Payload;

                // Create an XmlReader
                using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
                {
                    XmlWriterSettings ws = new XmlWriterSettings();
                    ws.Indent = true;
                    using (XmlWriter writer = XmlWriter.Create(output, ws))
                    {

                        // Parse the file and display each of the nodes.
                        while (reader.Read())
                        {
                            switch (reader.NodeType)
                            {
                                case XmlNodeType.Element:
                                    writer.WriteStartElement(reader.Name);                                    
                                    break;
                                case XmlNodeType.Text:
                                    writer.WriteString(reader.Value);
                                    break;
                                case XmlNodeType.XmlDeclaration:
                                case XmlNodeType.ProcessingInstruction:
                                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                                    break;
                                case XmlNodeType.Comment:
                                    writer.WriteComment(reader.Value);
                                    break;
                                case XmlNodeType.EndElement:
                                    writer.WriteFullEndElement();
                                    break;
                            }
                        }

                    }
                }

Open in new window

0
Comment
Question by:Meinhoonaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39852902
Could you reformulate your question?

parsing ? You mean you want to convert XML to JSON ?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 39852914
If you want to convert a XML document to JSON, you can use the SerializeXNode of the JSon.Net library. check http://emoreau.com/Entries/Articles/2014/01/Using-JsonNet.aspx
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39853036
Have you considered using JSON.Net available through NuGet?
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39853038
Sorry, Eric. I had this page opened for a while, and didn't notice you had actually suggested the same.
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question