简体   繁体   English

读取XML文件中的属性

[英]Reading attributes in an XML file

I am trying to read a file that looks like this: 我试图读取一个看起来像这样的文件:

<tasks>
  <task name="Project Management" mode="Automatic" start="07/01/2012 00:00" duration="21" id="954471332"></task>
  <task name="Conception/Approval" mode="Automatic" start="07/01/2012 00:00" duration="6" percentComplete="1" id="1905425539"></task>
  <task name="Define Initial Scope" start="07/04/2012 00:00" finish="07/18/2012 00:00" percentComplete="0.31" id="1154759651"></task>
</tasks>

I only want the values of name , start , and finish or duration , whichever exists. 我只想要namestartfinishduration ,以存在的为准。

This is what I have so far: 这是我到目前为止:

XElement allData = XElement.Load(dlg.FileName);
if (allData != null)
{
    IEnumerable<XElement> tasks = allData.Descendants("task");
    foreach (XElement task in tasks)
    {

    }
}

I'm sure I have to use the Attribute method but I'm not sure how to use it or the syntax. 我确定我必须使用Attribute方法,但我不确定如何使用它或语法。

You can do this to get the attributes: 您可以这样做以获取属性:

XElement allData = XElement.Load(dlg.FileName);
if (allData != null)
{
    IEnumerable<XElement> tasks = allData.Descendants("task");
    foreach (XElement task in tasks)
    {
        task.Attribute("name").Value;
        task.Attribute("start").Value;
        task.Attribute("finish").Value;
    }
}

I would recommend getting only the elements and values you need before looping through them: 我建议在循环之前只获取所需的元素和值:

XElement allData = XElement.Load(dlg.FileName);
if (allData != null)
{
  var tasks = allData.Descendants("task")
                     .Where(e => e.Attribute("name") != null
                                 && (e.Attribute("start") != null
                                     || e.Attribute("finish") != null))
                     .Select(e => new 
                     {
                       Name = e.Attribute("name").Value,
                       Start = e.Attribute("start").Value,
                       Finish = e.Attribute("finish").Value,
                     });
  foreach(var task in tasks)
  {
    // task.Name will have a value
    // task.Start and/or task.Finish will have a value.
  }
}

Assuming you have a object Tasks, how about something like: 假设你有一个对象任务,那么如下:

 XDocument doc = XDocument.Load(dlg.FileName);
  List<Task> infos = from c in doc.Descendants("task")
              select new Task (c.Element("name").Value, c.Element("start).Value, 
              c.Element("finish").Value);

You can do something like this: 你可以这样做:

var doc = XDocument.Load(dlg.FileName);
var query = doc.Descendants("task")
    .Select(task => new
    {
        Name = (string)task.Attribute("name"),
        Start = (DateTime)task.Attribute("start"),
        Finish = (DateTime?)task.Attribute("finish"),
        Duration = (long?)task.Attribute("duration"),
    });

The Finish or Duration fields will be null if the corresponding attribute does not exist. 如果相应的属性不存在,则FinishDuration字段将为null。

Then just loop through the items in the query to do as you wish. 然后只需遍历查询中的项目即可。

foreach (var item in query)
{
    var app = c1Schedule1.DataStorage.AppointmentStorage.Appointments.Add();
    app.Subject = item.Name;
    app.Start = item.Start;
    if (item.Finish != null)
    {
        app.Finish = item.Finish.Value;
    }
    if (item.Duration != null)
    {
        app.Duration = item.Duration.Value;
    }
}

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

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