2017-04-11 6 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((「ステータス」))に私が選択したノードを入れて、それを更新する必要があります。

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

次にループ:私は

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

    } 
} 
+0

XMLをオブジェクトに逆シリアル化しない理由は何ですか?オブジェクトのすべてのロジックを行い、オブジェクトをXMLにシリアル化しますか?それはあなたがsooooをもっと簡単に達成しようとしているものを作るでしょう。 –

+0

私はどうして、残念ですかわかりません。私はまだ学んでいる、これは私がXMLデータを処理する方法を知っている方法です。どうやって私に見せることができますか? –

+0

シリアル化/逆シリアル化の様子を示すために私の答えを更新しました。 –

答えて

0

だけで任意のコードを変更せずに、直接あなたの質問に答えるために - あなたはXPathを使用して、あなたのケースで特定のノードを選択したい場合は、あなたもそのようにそれを行うことができます。

もちろん
doc.SelectSingleNode("//new[1]/Status"); 

代わりに「1」のあなたが好きなインデックス(彼らはゼロベースではありません)

EDITを置くことができます:あなたは考慮のシリアライズ/デシリアライゼーションのに時間はかからなかったことをあなたのコメントで述べたように、私は強く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

、あなたがこのような何かにファイルを解析する方法を変更する必要があることを行うことができますどのように任意のアイデア解析されたツリーを介して、Node

そして最後に、XDocumentを保存:

doc.Save(Globals.pathNotifFile); 
1

まず下記のコードのように解析します