从XML提取数据到List. [英] Extracting Data from XML to List<>

查看:131
本文介绍了从XML提取数据到List.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<Record>
  <File name="1.mot">
    <Line address="040004" data="0720" />
    <Line address="040037" data="31" />
    <Line address="04004C" data="55AA55AA" />
  </File>
  <File name="2.mot">
    <Line address="00008242" data="06" />
    <Line address="00008025" data="AFC8" />
    <Line address="00009302" data="476F6C64" />
  </File>
</Record>

我要做的是从XML中提取信息并将其转换为列表.虽然我有点不知道从哪里开始以及如何开始.我已经搜索了样本,问题和下面的代码是到目前为止我设法构建的.我什至不确定这段代码是否适合我想发生的事情.该列表将用于程序中的某种查找.像在文件1.mot中一样,程序将读取1.mot,读取xml文件,解析两个文件,从xml文件中提取信息,然后执行搜索功能以验证xml中的信息是否存在于1.mot中.

What I want to do is to extract the information from the XML and convert that to list. Although I kind of don't know where and how to start. I've googled samples, and questions and the code below is what I've managed to construct so far. I'm not even sure if this code is appropriate for what I wanted to happen. This list will be used for some kind of lookup in the program. Like in file 1.mot, program would read 1.mot, read the xml file, parse both files, extract the info from the xml file and then do a search function to verify if the info in the xml exists in 1.mot.

XElement xmlReqs = XElement.Load("XMLFile1.xml");
List<Requirement> reqs = new List<Requirement>();
foreach (var xmlReq in xmlReqs.Elements("File"))
{
    string name = xmlReqs.Attribute("name").Value);
    List<InfoLine> info = new List<InfoLine>();
    foreach (var xmlInfo in xmlReq.Elements("Line"))
    {
      string address = xmlProduct.Attribute("address").Value;
      string data = xmlProduct.Attribute("data").Value;
    }
reqs.Add(new Requirement(address, data));
}

我的一个朋友建议了一些有关使用int数组或字符串数​​组,然后再使用此 reqs.Find(val => val[0]==target) 的方法,但是我不确定该怎么做.我对linq并不了解,但是我所收集的东西似乎非常引人注目且功能强大(?).

A friend of mine suggested something about using int array or string array and then using this reqs.Find(val => val[0]==target) but I'm not sure how to do so. I'm not well-versed with linq, but what I've gathered, it seems to be quite notable and powerful (?).

无论如何,上面的代码行得通吗?以及如何从列表中调用对象以用于程序的查找功能?

Anyway, will the code above work? And how do I call the objects from the list to use for the lookup function of the program?

更新: 程序将同时(或不)读取xml文件的1.mot或2.mot(取决于用户首选项,这就是为什么需要在xml中指定文件名的原因)的信息. 1.mot文件包含:

UPDATE: Program would be reading 1.mot or 2.mot (depending on the user preference, that's why file name in xml needs to be specified) simultaneously (or not) with the xml file. 1.mot file contains:

S0030000FC
S21404000055AA55AA072000010008000938383138D7
S21404001046305730343130302020202027992401B0
...

地址从第3个字节开始.是的,将数据与这些线段进行比较.

Address starts at the 3rd byte. So yeah, would be comparing the data to these bunch of lines.

推荐答案

您可以反序列化xml文件

You can de-serialize the xml file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Record));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            Record record = (Record)xs.Deserialize(reader);

        }
    }
    [XmlRoot("Record")]
    public class Record
    {
        [XmlElement("File")]
        public List<File> files {get;set;}
    }
    [XmlRoot("File")]
    public class File
    {
        [XmlAttribute("name")]
        public string name { get; set; }
        [XmlElement("Line")]
        public List<Line> lines {get;set;}
    }
    [XmlRoot("Line")]
    public class Line
    {
        [XmlAttribute("address")]
        public string address {get;set;}
        [XmlAttribute("data")]
        public string data {get;set;}
    }
}
​

这篇关于从XML提取数据到List.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆