如何使用C#将xml节点值读入字符串数组

时间:2016-05-26 10:53:14

标签: c# xml

我想读取xml节点值并存储在字符串数组中,我该怎么做

这是我的xml文件:

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

现在我想读取RULE1节点值并存储在字符串数组中。请任何人建议我如何做到这一点。

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个

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);
        }
    }

}
相关问题