2017-04-11 77 views
0

目前我交流#窗口工作表單應用程序,但我有以下問題:設定值

我已經在下面列出的xml:

<new> 
    <Company></Company> 
    <DateTime></DateTime> 
    <Message></Message> 
    <Status><Status> 
    </new> 
    <new> 
    <Company></Company> 
    <DateTime></DateTime> 
    <Message></Message> 
    <Status><Status> 
    </new> 
    <new> 
    <Company></Company> 
    <DateTime></DateTime> 
    <Message></Message> 
    <Status><Status> 
    </new> 
    <new> 
    <Company></Company> 
    <DateTime></DateTime> 
    <Message></Message> 
    <Status><Status> 
    </new> 
    <new> 
    <Company></Company> 
    <DateTime></DateTime> 
    <Message></Message> 
    <Status><Status> 
    </new> 

我得到的數據等這樣的:

XDocument doc = XDocument.Load(Globals.pathNotifFile); 
    var notifDateTime = doc.Descendants("DateTime"); 
    var message = doc.Descendants("Message"); 
    var company = doc.Descendants("Company"); 
    var sendStatus = doc.Descendants("Status"); 

    var dateTimeCollection = new List<String>(); 
    var messageCollection = new List<String>(); 
    var companyCollection = new List<String>(); 
    var statusCollection = new List<String>(); 

    foreach (var dateTimeOfNotification in notifDateTime) 
    { 
     dateTimeCollection.Add(dateTimeOfNotification.Value); 
    } 

    foreach (var messages in message) 
    { 
     messageCollection.Add(messages.Value); 
    } 

    foreach (var companys in company) 
    { 
     companyCollection.Add(companys.Value); 
    } 

    foreach (var isSent in sendStatus) 
    { 
     statusCollection.Add(isSent.Value); 
    } 

    return Tuple.Create(dateTimeCollection, messageCollection, companyCollection, statusCollection); 

而且我用xml文件

 Tuple<List<String>, List<String>, List<String>, List<String>> t = GetDataFromFile(); 
     List<String> dateTimeCollection = t.Item1; 
     List<String> messageCollection = t.Item2; 
     List<String> companyCollection = t.Item3; 
     List<String> statusCollection = t.Item4; 

     foreach (var notifDateTime in dateTimeCollection) 
     { 
      int index = dateTimeCollection.IndexOf(notifDateTime); 
      if (Int32.Parse(statusCollection[index]) == 1 || statusCollection[index] == string.Empty) 
      { 
       if (notifDateTime != string.Empty) 
       { 
        if (Convert.ToDateTime(notifDateTime) == DateTime.Now) 
        { 
         SendMessageToUser(messageCollection[index], companyCollection[index]); 
        } 
       } 
      } 
     } 
的數據這樣做

後,在SendMessageToUser我發送郵件,我得到迴應1,2或3,但我的問題是關於讓在那裏我必須寫狀態的確切節點。我用寫狀態的功能是:

XmlDocument doc = new XmlDocument(); 
    doc.LoadXml(Globals.pathNotifFile); 

    XmlNode commentsElement = doc.SelectSingleNode("Status"); 
    commentsElement.InnerText = status.ToString(); 

    doc.Save(Globals.pathNotifFile); 

所以在doc.SelectSingleNode((「狀態」))我必須把選定的節點和更新。任何想法我怎麼能做到這一點

+0

任何特別的原因,爲什麼你不反序列化的XML,以便對象,請在對象上所有的邏輯,序列化對象到XML?這會讓你想要實現的更輕鬆 –

+0

我不知道如何,對不起。我仍然在學習,這是我瞭解處理XML數據的方式。你能告訴我如何? –

+0

我更新了我的答案,向您展示序列化/反序列化的樣子 –

回答

0

就直接回答沒有任何代碼修改你的問題 - 如果你想使用XPath你的情況來選擇特定的節點,也可以那樣做:

doc.SelectSingleNode("//new[1]/Status"); 

當然不是「1」,你可以把你想要的任何指標(它們不是從零開始)

編輯:正如您在您的評論說,你沒有考慮到的序列化/反序列化,我強烈e鼓勵你嘗試使用它。讓你去定義你的數據如下:

[Serializable] 
public class root 
{ 
    [System.Xml.Serialization.XmlElementAttribute("new")] 
    public Node[] @new { get; set; } 
} 

[Serializable] 
public class Node 
{ 
    public string Status { get; set; } 
    public string Company { get; set; } 
    //other properties 
} 

現在你可以這樣做:

static void Main(string[] args) 
{ 
    var data = Deserialize(); 
    //do your logic on normal object 
    Serialize(data); 


} 

static root Deserialize() 
{ 
    using (var file = File.Open("test.txt", FileMode.Open)) 
    { 
     XmlRootAttribute xRoot = new XmlRootAttribute(); 
     xRoot.ElementName = "root"; 
     xRoot.IsNullable = true; 
     var xmlSerializer = new XmlSerializer(typeof(root), xRoot); 
     return (root)xmlSerializer.Deserialize(file); 
    } 
} 

static void Serialize(root data) 
{ 
    using (var file = File.Create("result.txt")) 
    { 
     var xmlSerializer = new XmlSerializer(typeof(root)); 
     xmlSerializer.Serialize(file, data); 
    } 
} 

只是一個例子,當然這是一個很大的話題,你可以在SO找到大量的實例和其他網站

1

我會解析像下面的代碼

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

namespace ConsoleApplication49 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 

      var results = doc.Descendants("new").Select(x => new { 
       company = (string)x.Element("Company"), 
       dateTime = (DateTime)x.Element("DateTime"), 
       message = (string)x.Element("Message"), 
       status = (string)x.Element("Status") 
      }).ToList(); 
     } 

    } 
} 
1

首先,你應該改變你的文件解析,以這樣的方式:

var newsDatas = xdoc.Descendants("new") 
       .Select(
        element => 
         new 
         { 
          Company = element.Element("Company").Value, 
          DateTime = element.Element("DateTime").Value, 
          Message = element.Element("Message").Value, 
          Node = element 
         }); 

然後循環通過解析樹並更新Status中的相關內容Node

foreach (var newsData in newsDatas) 
{ 
    // .. You logic 
    SendMessageToUser(newsData.Message, newsData.Company); 

    string status = ....; 
    newsData.Node.Element("Status").Value = status; 
} 

而在去年,保存的XDocument:

doc.Save(Globals.pathNotifFile);