简体   繁体   中英

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.

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.

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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