[英]Unity XML Get list values from xml list to c# list
I need to retrieve list values from attributes in a file with this XML:我需要使用此 XML 从文件中的属性中检索列表值:
<LISTS>
<LIST list="213 82 14 2 3 4 18 4 1 5 5 3 6 5 2"/>
<LIST list="2 4 5 1 4 0 0 0 2 3 5 532 7 10 0"/>
<LIST list="10 511 4 8 2 6 4 721 2 5 7 10 10 4"/>
</LISTS>
Using a stream reader I put the XML in a string inside a list.使用 stream 阅读器,我将 XML 放入列表中的字符串中。 So it looks like this:所以它看起来像这样:
<LISTS><LIST list="213 82 14 2 3 4 18 4 1 5 5 3 6 5 2"/><LIST list="2 4 5 1 4 0 0 0 2 3 5 532 7 10 0"/><LIST list="10 511 4 8 2 6 4 721 2 5 7 10 10 4"/></LISTS>
Then I create the XML document然后我创建 XML 文档
Xmldoc = new XmlDocument();
Xmldoc.LoadXml(XmlLineList[0]) //i get an obj reference not set to instance of obj ex here. Even tho i can see in the inspector the xml line is there.
If I use如果我使用
Xmldoc.Load(XmlLineList[0]) // Illegal characters in Path
And that is as far as I have gone.这就是我所经历的。 I have been reading documentation all night but cant find a good answer or a case that resembles mine.我整晚都在阅读文档,但找不到一个好的答案或类似于我的案例。 Need to pass the elements of the xml list into a c# list.需要将 xml 列表的元素传递到 c# 列表中。 Every list in a different one, so three lists.每个列表都在一个不同的列表中,所以三个列表。
Your XML basically consists of a sequence <LIST>
of lists list="1 2 3"
of integers.您的 XML 基本上由一系列<LIST>
的列表list="1 2 3"
整数组成。 You have a couple options to parse this XML and extract a List<int>
list for each <LIST>
element.您有几个选项来解析这个 XML 并为每个<LIST>
元素提取一个List<int>
列表。
Firstly, You can use XmlSerializer
to deserialize your XML.首先,您可以使用XmlSerializer
反序列化您的 XML。 First define the followng data model:首先定义如下数据model:
[XmlRoot("LISTS")]
public class ListRoot
{
[XmlElement("LIST")]
public List<ListEntry> Lists { get; set; } = new List<ListEntry>();
}
public class ListEntry
{
[XmlAttribute("list")]
public List<int> ListItems { get; set; } = new List<int>();
}
Then you can deserialize from a file located at fileName
as follows:然后您可以从位于fileName
的文件反序列化,如下所示:
ListRoot listRoot;
var serializer = new XmlSerializer(typeof(ListRoot));
using (var stream = File.OpenRead(fileName))
listRoot = (ListRoot)serializer.Deserialize(stream);
By marking the List<int> ListItems
with [XmlAttribute("list")]
, XmlSerializer
will automatically deserialize the space-delimited list="2 4..."
attributes into the inner ListItems
list.通过使用[XmlAttribute("list")]
标记List<int> ListItems
, XmlSerializer
将自动将空格分隔的list="2 4..."
属性反序列化到内部ListItems
列表中。
Now you can access the list items as follows:现在您可以按如下方式访问列表项:
foreach (var entry in listRoot.Lists)
{
List<int> list = entry.ListItems; // The contents of each list="1 2 ..." /> attribute materialized as a List<int>
// Process the List<int> list however you want:
Console.WriteLine(string.Join(",", list));
}
Which outputs:哪个输出:
213,82,14,2,3,4,18,4,1,5,5,3,6,5,2
2,4,5,1,4,0,0,0,2,3,5,532,7,10,0
10,511,4,8,2,6,4,721,2,5,7,10,10,4
Demo fiddle #1 here .演示小提琴#1在这里。
Alternatively, you could use LINQ to XML to load your XML into an XDocument
and query it using LINQ :或者,您可以使用LINQ 到 XML 将 XML加载到XDocument
中并使用Z979E0678FD89D7415C :22 查询它
var listRoot = XDocument.Load(fileName);
var listItems = listRoot.Root
.Elements("LIST") // Select the <LIST> child nodes
.Select(e => e.Attribute("list")) // Select the list=".." attribute
.Select(a => a.Value.Split(' ').Select(s => XmlConvert.ToInt32(s)).ToList()) // Convert the space-delimited integer string to a list of integers
.ToList(); // And materialize the query
foreach (List<int> list in listItems)
{
// Process the List<int> list however you want:
Console.WriteLine(string.Join(",", list));
}
Demo fiddle #2 here .演示小提琴#2在这里。
I don't really recommend using the older XmlDocument
for new code as LINQ to XML is easier to use and more performant.我真的不建议将旧的XmlDocument
用于新代码,因为 LINQ 到 XML 更易于使用且性能更高。
It is better to use LINQ to XML API.最好使用 LINQ 到 XML API。 It is available in the.Net Framework since 2007.它自 2007 年起在 .Net Framework 中可用。
Here is your starting point.这是您的起点。
c# c#
void Main()
{
const string fileName = @"e:\temp\input.xml";
// Load XML file directly from the file system
//XDocument xdoc = XDocument.Load(fileName);
XDocument xdoc = XDocument.Parse(@"<LISTS>
<LIST list='213 82 14 2 3 4 18 4 1 5 5 3 6 5 2'/>
<LIST list='2 4 5 1 4 0 0 0 2 3 5 532 7 10 0'/>
<LIST list='10 511 4 8 2 6 4 721 2 5 7 10 10 4'/>
</LISTS>
");
foreach (XElement xelem in xdoc.Descendants("LIST"))
{
Console.WriteLine("list: '{0}'", xelem.Attribute("list").Value);
}
}
Output Output
list: '213 82 14 2 3 4 18 4 1 5 5 3 6 5 2'
list: '2 4 5 1 4 0 0 0 2 3 5 532 7 10 0'
list: '10 511 4 8 2 6 4 721 2 5 7 10 10 4'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.