[英]Im parsing some text from xml file how can i parse also the names?
This is the function: 这是功能:
private void ParseAndDisplayXml(string filename)
{
XDocument document = XDocument.Load(filename);
var list = document.Root.Elements("Message")
.Select(
e => new
{
Date = e.Attribute("Date").Value.ToString(),
Time = e.Attribute("Time").Value.ToString(),
Text = e.Element("Text").Value.ToString(),
Name = e.Element("FriendlyName").Value.ToString()
}
);
string result="";
foreach (var item in list)
{
result += string.Format("Date--{0},Time--{1},Text--{2},Name--{3}", item.Date, item.Time, item.Text, item.Name + Environment.NewLine);
}
textBox1.Text = result;
}
Im getting null on the Name variable in this part: 我在这部分的Name变量上得到null:
{
Date = e.Attribute("Date").Value.ToString(),
Time = e.Attribute("Time").Value.ToString(),
Text = e.Element("Text").Value.ToString(),
Name = e.Element("FriendlyName").Value.ToString()
}
This is some content from the xml file: 这是xml文件中的一些内容:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='MessageLog.xsl'?>
<Log FirstSessionID="1" LastSessionID="2"><Message Date="31/01/2012" Time="10:15:42" DateTime="2012-01-31T08:15:42.467Z" SessionID="1"><From><User FriendlyName="Chocolade"/></From><To><User FriendlyName="adilipman@yahoo.com"/></To><Text Style="font-family:Segoe UI; color:#000000; ">היי</Text></Message><Message Date="31/01/2012" Time="10:15:55" DateTime="2012-01-31T08:15:55.097Z" SessionID="1"><From><User FriendlyName="Chocolade"/></From><To><User FriendlyName="adilipman@yahoo.com"/></To><Text Style="font-family:Segoe UI; color:#000000; ">הייתה לי בעיה עם התוכנת ברקים אבל עכשיו הכל עובד.</Text></Message><Message Date="31/01/2012" Time="10:15:58" DateTime="2012-01-31T08:15:58.897Z" SessionID="1"><From><User FriendlyName="Chocolade"/></From><To><User FriendlyName="adilipman@yahoo.com"/></To><Text Style="font-family:Segoe UI; color:#000000; ">מה השלב הבא ?</Text></Message><Message Date="31/01/2012" Time="10:16:27" DateTime="2012-01-31T08:16:27.775Z" SessionID="1"><From><User FriendlyName="Chocolade"/></From><To><User FriendlyName="adilipman@yahoo.com"/></To><Text Style="font-family:Segoe UI; color:#000000; ">אמרת לי בזמנו לחשב ממוצע של 1000 ערכים ? הכוונה 1000 ערכים בפריים ? כי בפריים יש 256 מספרים לא ?</Text></Message><Message Date="31/01/2012" Time="10:17:03" DateTime="2012-01-31T08:17:03.405Z" SessionID="1"><From><User FriendlyName="adilipman@yahoo.com"/></From><To><User FriendlyName="Chocolade"/></To><Text Style="font-family:Segoe UI; color:#000000; ">לחשב ממוצע של 1000 הערכים הגבוהים ביותר בהיסטוגרמה</Text></Message><Message Date="31/01/2012" Time="10:17:10" DateTime="2012-01-31T08:17:10.405Z" SessionID="1"><From><User FriendlyName="adilipman@yahoo.com"/></From><To><User FriendlyName="Chocolade"/></To><Text Style="font-family:Segoe UI; color:#000000; ">ז"א בפריים</Text></Message><Message Date="31/01/2012" Time="10:17:14" DateTime="2012-01-31T08:17:14.135Z" SessionID="1"><From><User FriendlyName="adilipman@yahoo.com"/></From><To><User FriendlyName="Chocolade"/></To><Text Style="font-family:Segoe UI; color:#000000; ">לא בהיסטוגרמה</Text></Message><Message Date="31/01/2012" Time="10:17:20" DateTime="2012-01-31T08:17:20.142Z" SessionID="1"><From><User FriendlyName="adilipman@yahoo.com"/></From><To><User FriendlyName="Chocolade"/></To><Text Style="font-family:Segoe UI; color:#000000; ">בעזרת ההיסטוגרמה</Text></Message><Message Date="31/01/2012" Time="10:17:50" DateTime="2012-01-31T08:17:50.648Z" SessionID="1"><From><Us
What i want to do is that it will show me: adilipman hello Then: chocolade hi 我想做的是,它将向我显示:adilipman你好然后:chocolade嗨
For now what im getting is a date time and text but i want to add also the name for each line and text so i know who said it. 目前,即时消息是日期时间和文本,但我还想为每行和文本添加名称,以便我知道谁说的。
FrindlyName不是元素user的属性,我假设它来自from元素,而不是to元素。
e.Element("From").Element("User").Attribute("FriendlyName").Value.ToString()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.