繁体   English   中英

如何将XML转换为List <string> 还是String []?

[英]How can I transform XML into a List<string> or String[]?

如何将以下XML转换为List<string>String[]

<Ids>
  <id>1</id>
  <id>2</id>
</Ids>

听起来你只是解析而不是完整的XML序列化/反序列化。 如果您可以使用LINQ to XML,这非常简单:

using System;
using System.Linq;
using System.Xml.Linq;

public class Test
{
    static void Main()
    {
        string xml = "<Ids><id>1</id><id>2</id></Ids>";

        XDocument doc = XDocument.Parse(xml);

        var list = doc.Root.Elements("id")
                           .Select(element => element.Value)
                           .ToList();

        foreach (string value in list)
        {
            Console.WriteLine(value);
        }
    }
}

事实上调用Elements可以省略该参数,因为只有 id的元素,但我想我会演示如何指定你想要的元素。

同样地,我通常不会打扰调用ToList除非我真的需要一个List<string> - 没有它,结果是IEnumerable<string>如果你只是迭代它一次就没问题。 要改为创建数组,请使用ToArray

以下是使用XmlDocument的方法:

// A string containing the XML data
string xml = "<Ids><id>1</id><id>2</id></Ids>";
// The list you want to fill
ArrayList list = new ArrayList();

XmlDocument doc = new XmlDocument();
// Loading from a XML string (use Load() for file)
doc.LoadXml(xml); 
// Selecting node using XPath syntax
XmlNodeList idNodes = doc.SelectNodes("Ids/id");
// Filling the list
foreach (XmlNode node in idNodes)
    list.Add(node.InnerText);

任何类型的集合。 例如 :

<Ids> <id>C:\\videotest\\file0.txt</id> <id>C:\\videotest\\file1.txt</id> </Ids>

class FileFormat
    {
        public FileFormat(string path)
        {
            this.path = path;
        }
        public string FullPath
        {
            get { return Path.GetFullPath(path); }
        }
        public string FileName
        {
            get { return Path.GetFileName(path); }
        }
        private string path;
    }

List<FileFormat> Files =
        selectedNode
        .Descendants("Ids")
        .Elements("Id")
        .Select(x => new FileFormat(x.Value))
        .Where(s => s.FileName!=string.Empty)
        .ToList();

将字符串数组转换为xml的代码

items是一个字符串数组

items.Aggregate("", (current, x) => current + ("<item>" + x + "</item>"))

此示例将与.NET framework 4.0一起使用:

进入清单

    List<string> Ids= new List<string>(); 
    Ids= selectedNode.Descendants("Ids").Elements("Id").Select(> x=>x.Value).Where(s =>s!= string.Empty).ToList();

成一个字符串[]

    string[] Ids= thisNode
                  .Descendants("Ids")          // Ids element
                  .Elements("Id")              // Id elements
                  .Select(x=>x.Value)          // XElement to string
                  .Where(s =>s!= string.Empty) // must be not empty
                  .ToArray();                  // string to string []

此示例将与.NET framework 3.5一起使用:

    System.Xml.Linq.XElement element = System.Xml.Linq.XElement.Parse("<Ids>  <id>1</id>  <id>2</id></Ids>");
    System.Collections.Generic.List<string> ids = new System.Collections.Generic.List<string>();
    foreach (System.Xml.Linq.XElement childElement in element.Descendants("id"))
    {
        ids.Add(childElement.Value);
    }

您可以使用Properties类。

Properties prop = new Properties();
prop.loadFromXML(stream);
Set set = prop.keySet();

您可以从每个键的集合中访问字符串。 键是xml的元素名称。

这是一种使用DataSet从xml获取类型化数组的方法。 (在此示例中为双精度数组)

DataSet dataSet = new DataSet()
DoubleArray doubles = new DoubleArray(dataSet,0);

dataSet.ReadXml("my.xml");
double a = doubles[0];



public class DoubleArray
{
  DataSet dataSet;
  int tableIndex;
  public DoubleArray(DataSet dataSet,int tableIndex)
  {
    this.dataSet=dataSet;
    this.tableIndex=tableIndex;
  }

  public double this[int index]
  {
    get
    { 
      object ret = dataSet.Tables[tableIndex].Rows[index];
      if(ret is double?) 
        return (ret as double?).Value;
      else
        return double.Parse(ret as string);
    }
    set
    {
      object out = dataSet.Tables[tableIndex].Rows[index];
      if(out is double?)
        dataSet.Tables[tableIndex].Rows[index] = (double?)value;
      else
        dataSet.Tables[tableIndex].Rows[index] = value.ToString();
    }
  }
}

当然,解析双打并将它们一直转换回字符串可能会被一些程序员视为亵渎......即使对我来说也很难不去考虑这样的资源浪费......但我猜有时候最好转过来另一个离开..不要强调它:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM