には、アプリケーションを構成するが、私は次のような問題があります。設定値
私は、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();
}
}
}
XMLをオブジェクトに逆シリアル化しない理由は何ですか?オブジェクトのすべてのロジックを行い、オブジェクトをXMLにシリアル化しますか?それはあなたがsooooをもっと簡単に達成しようとしているものを作るでしょう。 –
私はどうして、残念ですかわかりません。私はまだ学んでいる、これは私がXMLデータを処理する方法を知っている方法です。どうやって私に見せることができますか? –
シリアル化/逆シリアル化の様子を示すために私の答えを更新しました。 –