0

I want to read xml node values and store in string array, how can I do this

Here is my xml file:

<SET_RULES> 
<DOMAIN_RULES> 
<RULE1>user1,user2,user3,user4</RULE1> 
<RULE2>test2</RULE2> 
<RULE3>test3</RULE3> 
<RULE4>test4</RULE4> 
</DOMAIN_RULES> 
</SET_RULES>

now I want to read RULE1 node values and to store in string array. Please any one suggest me how to do this.

Thanks in advance.

1 Answers1

0

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication93
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<SET_RULES>" +
                    "<DOMAIN_RULES>" +
                    "<RULE1>user1,user2,user3,user4</RULE1>" +
                    "<RULE2>test2</RULE2>" +
                    "<RULE3>test3</RULE3>" +
                    "<RULE4>test4</RULE4>" +
                    "</DOMAIN_RULES>" +
                "</SET_RULES>";

            XElement rules = XElement.Parse(xml);

            string output = string.Join(",",
                rules.Descendants("DOMAIN_RULES").Elements().Select(x => (string)x).ToArray());
            string[] outputArray = output.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20