Solved

how to parse xml attributes to JSON

Posted on 2014-02-11
4
591 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 69

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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 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…

895 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

12 Experts available now in Live!

Get 1:1 Help Now