2012-07-10 30 views
0

讀取屬性我想讀,看起來像這樣的文件:在一個XML文件

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

我只想要namestartfinishduration,兩者存在的價值。

這是我到目前爲止有:

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

    } 
} 

我敢肯定,我必須使用屬性的方法,但我不知道如何使用它或語法。

+0

<任務名稱= 「定義初始範圍」 開始= 「07月04日00:00」 光潔度= 「2012年7月18日00:00」 PERCENTCOMPLETE = 「0.31」 ID = 「1154759651」> 這些是我的任務XML文件 – user1515742 2012-07-10 18:50:46

+0

您的示例XML沒有顯示在您的問題中?你能嘗試把它放進去嗎? – 2012-07-10 18:51:10

+0

它在現在,不會接受主標籤 – user1515742 2012-07-10 18:52:50

回答

1

你可以做到這一點,以獲得屬性:

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

難道你不想使用'allData.Elements(「task」)'更具體。 – abatishchev 2012-07-10 18:58:44

+0

當我將這些添加到我的項目中時,出現錯誤 「只有賦值,調用,增量,減量和新的對象表達式可以用作語句」 – user1515742 2012-07-10 18:58:56

+0

如果其中一個屬性不存在,將會崩潰可以根據問題發生。爲屬性添加一個空檢查。 – 2012-07-10 19:02:38

0

假設你有一個目標任務,怎麼樣是這樣的:

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); 
1

我建議只得到元素和值需要通過這些循環之前:

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. 
    } 
} 
+0

你在做什麼? – user1515742 2012-07-10 19:05:23

+0

我不確定你的意思是什麼意思。在我的例子中,值'e'可以是任何東西,它只是一個lambda參數,在這個例子中表示一個'XElement'的值。 – 2012-07-10 19:16:40

0

你可以做這樣的事情:

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"), 
    }); 

FinishDuration字段將是無效如果相應的屬性不存在。

然後,只需循環瀏覽查詢中的項目即可。

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

如果我想使用查詢變量它的語法看起來像什麼,說我即將做一個新的約會C1Schedule: '代碼' 約會應用程序; app = c1Schedule1.DataStorage.AppointmentStorage.Appointments.Add(); app.Subject = query.Select ; 'code' 我試過query.select 但顯然不是這樣。 – user1515742 2012-07-10 19:21:05

+0

這是一個查詢,所以你必須遍歷它並從查詢中的項目中獲取值。我已經展示了你如何做的一個例子。 – 2012-07-10 19:25:28

+0

希望你能回答的另一個簡短問題。我以這種方式做了我的輸入,你向我展示了一切正常。假設我想要一個按鈕來使用甘特圖來更新我的計劃。當我點擊按鈕即時試圖保存XML文件,然後重新加載它使用這種方法,唯一的事情是,當我去加載它,我得到一個ArgumentNullException後的「新」開始。選擇 – user1515742 2012-07-16 15:32:56