使用c#将xml读入字符串[] [英] using c# to read xml into a string[]
问题描述
我有
      string xml =
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "<?xml version = \" 1.0 \"编码= \" UTF-8\"?>" +
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "< folder name = \" c \">" +
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "< folder name = \" program files \">" +
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "< folder_name = \" uninstall information \" />" +
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "< /文件夹>" +
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "< folder name = \" users \" />" +
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "< / folder>";
string xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<folder name=\"c\">" +
"<folder name=\"program files\">" +
"<folder_name=\"uninstall information\" />" +
"</folder>" +
"<folder name=\"users\" />" +
"</folder>";
我试图把它读成像这样的字符串数组
I am trying to read it into a string array like this
string [] myList = new string [3]; // {" c"," program files"," uninstall information"," users" };
string[] myList = new string[3];// { "c", "program files", "uninstall information", "users" };
我该怎么做?
推荐答案
Hello robMerritt,
Hello robMerritt,
使用
linq to xml 。
string xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<folder name=\"c\">" +
"<folder name=\"program files\">" +
"<folder name=\"uninstall information\" />" +
"</folder>" +
"<folder name=\"users\" />" +
"</folder>";
XDocument xd = XDocument.Parse(xml);
string[] str=xd.Descendants("folder").Select(x => x.Attribute("name").Value).ToArray();
注意。您提供的xml源格式错误,我编辑了一点。
Note. The xml source you provided has wrong format ,I have edited a little.
此致,
Neil Hu
这篇关于使用c#将xml读入字符串[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!