Solved

how to parse xml attributes to JSON

Posted on 2014-02-11
4
589 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
Comment Utility
Could you reformulate your question?

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

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
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
Comment Utility
Have you considered using JSON.Net available through NuGet?
0
 
LVL 23

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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)
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now