读书人

怎么将XML里的东西写入一个数组

发布时间: 2012-03-25 20:55:17 作者: rapoo

如何将XML里的东西写入一个数组?
假如我有一个XML文件:
<Propertys Name ="myName">
<Data Value="11"/>
<Data Value="22"/>
<Data Value="33"/>
</Propertys>
现在我定义一个数组 string[] PropertyValue;
那么我怎么将XML里面 Data的值全部列到PropertyValue里面去
注:不能直接 string[] PropertyValue={"11","22","33"};我要动态的。
没多少分了,跪求!!!


[解决办法]
XmlNodeList nodes = doc.SelectNodes("/Propertys/Data/@Value")
string[] PropertyValue= new String[nodes.Count]
for(int i=0;i<nodes.Count;i++)
PropertyValue[i]=nodes[i].Value
[解决办法]

C# code
  List<string> PropertyValue= new List<string>();            string xmlstr = "<?xml version=\"1.0\"?>"+                            "<Propertys Name =\"myName\">" +                              "<Data Value=\"11\"/>" +                              "<Data Value=\"22\"/>" +                              "<Data Value=\"33\"/>" +                            "</Propertys>";            XmlDocument xd = new XmlDocument();            xd.LoadXml(xmlstr);            XmlNodeList xl = xd.GetElementsByTagName("Data");            for (int i = 0; i < xl.Count; i++)            {                PropertyValue.Add(xl[i].Attributes["Value"].Value);            }
[解决办法]
C# code
  List<string> value_list = new List<string>();                XmlDocument xmlDoc = new XmlDocument();                xmlDoc.Load(@"C:\Documents and Settings\Administrator\桌面\Test.txt");                foreach (XmlNode xn in xmlDoc.SelectNodes("//Propertys/Data"))                {                    value_list.Add(xn.Attributes["Value"].Value);                }                /*                 *         [0]    "11"    string                        [1]    "22"    string                        [2]    "33"    string                 */
[解决办法]
C# code
<?xml version="1.0" encoding="utf-8" ?><Propertys Name ="myName">  <Data Value="11"/>  <Data Value="22"/>  <Data Value="33"/></Propertys>以下是读取代码:using System.Xml.Linq;using System.Xml;using System.Text;        StringBuilder sb = new StringBuilder();        string[] PropertyValue;        XmlDocument xml = new XmlDocument();        xml.Load(Server.MapPath("test.xml"));        XmlNodeList xn = xml.SelectNodes("Propertys/Data");        foreach (XmlNode xn1 in xn)        {            sb.Append(xn1.Attributes["Value"].Value+",");        }        PropertyValue = sb.ToString().TrimEnd(',').Split(',');        foreach (string s in PropertyValue)        {            Response.Write(s+"</br>");        }
[解决办法]
探讨

C# code
List<string> value_list = new List<string>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\Documents and Settings\Administrator\桌面\Test.txt");

……

[解决办法]
都很详细了,,,接分。。

读书人网 >C#

热点推荐