Solved

how to parse xml attributes to JSON

Posted on 2014-02-11
4
592 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
  • 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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now