Windex
Windex

Reputation: 143

c# XML Parsing separating innerxml from innertext

What I am trying to do is create ideally a nested List basically a 2d list, or a 2D array if that is better for this task, that would work as follows ID => 1 Name => Hickory without explicitly selecting the node.

I could use SelectNode (Woods/Wood) and then do something like node["ID"].InnerText but that would require that I know what the nodes name is.

Assume that this would read wood.xml even if there were 36 nodes instead of 7 and that I will never know the name of the nodes. I tried using outerxml/innerxml but that gives me too much information.

 XmlDocument doc = new XmlDocument();
        doc.Load("wood.xml");

        //Here is wood.xml
        /*<Woods><Wood><ID>1</ID><Name>Hickory</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>2</ID><Name>Soft Maple</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>3</ID><Name>Red Oak</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood></Woods>*/


        XmlNode root = doc.FirstChild;

        //Display the contents of the child nodes.
        if (root.HasChildNodes)
        {
            for (int i=0; i<root.ChildNodes.Count; i++)
            {
                Console.WriteLine(root.ChildNodes[i].InnerXml);
                Console.WriteLine();
            }

            Console.ReadKey();
        }

That would allow me to basically create a wood "buffer" if you will so I can access these values elsewhere.

Sorry if I was unclear I want to essentially make this "abstract" for lack of a better word.

So that if I were someday to change the name of "Weight" to "HowHeavy" or if i were to add an additional element "NumberOfBranches" I would not have to hardcode the structure of the xml file.

Upvotes: 1

Views: 3199

Answers (2)

Petr Behensk&#253;
Petr Behensk&#253;

Reputation: 620

You can use xmlDocument.SelectNodes("//child::node()")

Upvotes: 1

Surjit Samra
Surjit Samra

Reputation: 4662

Is this what you after ?

class Program
    {   
        static void Main(string[] args)
        {
          string xml = @"<Woods><Wood><ID>1</ID><Name>Hickory</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>2</ID><Name>Soft Maple</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>3</ID><Name>Red Oak</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood></Woods>";

           XDocument doc = XDocument.Parse(xml);
           //Get your wood nodes and values in a list 
           List<Tuple<string,string>> list = doc.Descendants().Select(a=> new Tuple<string,string>(a.Name.LocalName,a.Value)).ToList();

           // display the list
           list.All(a => { Console.WriteLine(string.Format("Node name {0} , Node Value {1}", a.Item1, a.Item2)); return true; });
           Console.Read();
        }
    }  

Upvotes: 3

Related Questions