0

How can I deserialize this XML in C#?

I want to print all strings of each ArrayOfString in a line.

How can I do that?

<ArrayOfArrayOfString 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://library.org">

    <ArrayOfString>
        <string>1</string>
        <string>Jack</string>
        <string>The Alchemist</string>
    </ArrayOfString>
    <ArrayOfString>
        <string>2</string>
        <string>Henry</string>
        <string>The Catcher In The Rye</string>
    </ArrayOfString>
</ArrayOfArrayOfString>

p.s.: The above XML is a response from a web service, so suppose that I have it as a string. I don't wanna use Serializable classes and blah blah, I JUST wanna iterate over it, and print out children. I'm new to this so please don't confuse me with irrelevant URLs. (In that URL XML is in a path, not a string, and children are not identical.) Thanks

Chainsw
  • 37
  • 7
  • 1
    Load this string of XML into a `XmlDocument` object, use XPath to select the `ArrayOfString` nodes and print its children. – Jack Le Aug 19 '19 at 12:06
  • 1
    If you'd rather have a C# class you can deserialize it into, Visual Studio has a neat feature. You can put the XML into the clipboard and then use Edit -> Paste Special -> Paste XML as classes. Then it'll generate C# classes that match the XML. – Hans Kilian Aug 19 '19 at 12:22
  • @HansKilian thank you for that tip. It seems to have a little difficulties with the naming, but it looks comfortable – Mong Zhu Aug 19 '19 at 12:48
  • 1
    @MongZhu I saw that URL, don't make that complicated with Serializable classes and ..., I just wanna print childs. Is there any simpler way? – Chainsw Aug 20 '19 at 15:20

1 Answers1

1

Using this as your data:

<ArrayOfArrayOfString 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://library.org">

    <ArrayOfString>
        <string>1</string>
        <string>Jack</string>
        <string>The Alchemist</string>
    </ArrayOfString>
    <ArrayOfString>
        <string>2</string>
        <string>Henry</string>
        <string>The Catcher In The Rye</string>
    </ArrayOfString>
</ArrayOfArrayOfString>

This code:

using System;
using System.Xml;

namespace XML_57556340
{
    class Program
    {
        static void Main(string[] args)
        {
            doitagain("M:\\StackOverflowQuestionsAndAnswers\\XML_57556340\\Data.xml");
            Console.ReadLine();
        }


        private static void doitagain(string v)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(v);
            foreach (XmlNode item in xdoc.GetElementsByTagName("ArrayOfString"))
            {
                foreach (XmlElement ele in item.ChildNodes)
                {
                    Console.Write(ele.InnerText);
                }
                Console.WriteLine();
            }
        }
    }
}

yields this:

1JackThe Alchemist
2HenryThe Catcher In The Rye
blaze_125
  • 2,262
  • 1
  • 9
  • 19