我如何从xml文件中只读取文本逐行从xml文件解析日期和时间?

时间:2012-08-23 01:30:25

标签: c#

在此代码中,我使用InnerText仅查看xml文件中没有任何标记的文本。 但我现在想要改变两件事:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string xmlString = @"d:\adilipman1937067724.xml";

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlString);
            string t = doc.InnerText;
            textBox1.Text = t;
        }
    }
}
  1. 将文本解析为行。

  2. 要解析并向每行添加日期和时间。

  3. 以下是我要解析的xml文件的示例:

    FriendlyName="adilipman@yahoo.com"/></To><Text Style="font-family:Segoe UI; color:#000000; ">testing the test.</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 
    

    相反,只在文本中添加“测试测试”,我希望它在textBox1中格式化为:

    adilipman 10:15:58 31/01/2012 testing the test      
    

    这是一条线。 下一行将是例如:

    adilipman 10:15:59 31/01/2012 testing the test was ok 
    

    但是现在我在textBox1中得到的是这样的:

    testing the test testing the test was ok 
    

    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 
    

1 个答案:

答案 0 :(得分:0)

解析xml文档有很多不同的方法...使用XmlDocumentXmlTextReader但我更喜欢使用Linq到xml类来解析现有的xml文档,但是如果必须的话创建我的oun xml文档,然后解析它我更喜欢使用序列化。我不是因为我不知道你把xml文件带到了哪里?你自己装箱了吗???如果是,则应使用xml序列化将自己的对象保存到xml,然后反序列化以将其读回。它比解析xml文档容易得多。

以下是与您的问题类似的最简单示例: 正如我从你的xml例子中可以理解的那样......我使用了这个xml片段:

<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 >testing the test.</Text>
</Message>

您应该使用DateTime,SessionId,From,To和Text字段创建Message类。类似的东西:

[Serializable]
public class User
{
      public string FriendlyName { get; set; }
}

[Serializable]
public class Message
{
      public string Text { get; set; }
      public DateTime DateTime { get; set; }
      public int SessionID { get; set; }
      public User Form { get; set; }
      public User To { get; set; }
}

[Serializable]属性是编译器知道您的类型(消息)可以序列化所必需的。在创建类xml结构后,您可以序列化Message或它们的List<Message>的任何实例。你应该做下一个:

private void button1_Click(object sender, EventArgs e)
{
string xmlToSerializePath = @"c:/temp/stackowrflowquestionxml.xml";

XmlSerializer serializer = new XmlSerializer(typeof(Message));
StreamWriter writer = new StreamWriter(xmlToSerializePath);

Message messageToSerialize = new Message
                                    {
                                        DateTime = new DateTime(2012, 1, 31, 8, 15, 58),
                                        Form = new User()
                                                  {
                                                        FriendlyName = "Chocolade"
                                                  },
                                        To = new User
                                                  {
                                                        FriendlyName = "adilipman@yahoo.com"
                                                  },
                                        SessionID = 1,
                                        Text = "testing the test."
                                     };
       serializer.Serialize(writer, messageToSerialize);
       writer.Close();
}

您将获得下一个xml文档:

<?xml version="1.0" encoding="utf-8"?>
<Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Text>testing the test.</Text>
  <DateTime>2012-01-31T08:15:58</DateTime>
  <SessionID>1</SessionID>
  <Form>
    <FriendlyName>Chocolade</FriendlyName>
  </Form>
  <To>
    <FriendlyName>adilipman@yahoo.com</FriendlyName>
  </To>
</Message>

它与你的非常相似,但现在如果你需要获取有关该消息的所有信息,你不需要使用xml,你只需要反序列化它并获得你自己的Message类的正确实例:

StreamReader reader = new StreamReader(xmlToSerializePath);
Message deserializedMessage = (Message)serializer.Deserialize(reader);
reader.Close();

当您获得邮件实例时,您可以按照自己的意愿操作邮件,并在文本框中填写邮件的任何信息:

textBox1.Text = string.Format("{0} {1} {2} {3}", 
          deserializedMessage.DateTime, 
          deserializedMessage.Form.FriendlyName, 
          deserializedMessage.To.FriendlyName, 
          deserializedMessage.SessionID);

文本框文本如下:

31.01.2012 8:15:58 Chocolade adilipman@yahoo.com 1

/ =========================================== ======================================= /

如果您不喜欢或不想使用此类方法,或者您只需要解析已由某些第三人称xml创建,则可以使用linq to xml语法。例如:

创建XDocument实例:

string xmlStringPath = @"d:/adilipman1937067724.xml";
XDocument document = new XDocument(xmlStringPath);

但有时它可以抛出异常,因此您需要创建XmlDocument,将xml作为字符串加载,并使用静态XDocument方法来解析该字符串:

XmlDocument doc = new XmlDocument();
doc.Load(xmlString);
XDocument document = XDocument.Parse(doc.OuterXml);

注意XDocument和XmlDocument。这是两种不同的类型。然后,您可以使用linq语法解析文档:

// Get root message element
var messageElement = (from element in document.Elements()
                                  where element.Name == "Message"
                                  select element).FirstOrDefault();

// Get date attribute of message element
string date = messageElement.Attribute("Date").Value;

// Get To element
// Notice that we make select from elements of message element messageElement.Elements()
// You should closely follow about what attribute are you making select from,
// because your selection will return null, and you will get errors.
var toElement = (from element in messageElement.Elements()
                             where element.Name == "To"
                                select element).FirstOrDefault();

// Then we take value of user element friendly name attribute
// We get element (tag) User of all possible existing sub tags of tag To
string toFriendlyName =
                (from element in toElement.Elements()
                 where element.Name == "User"
                 select element.Attribute("FriendlyName").Value).FirstOrDefault();

等等每个属性或元素。正如你所看到的,用手解析xml是非常困难和笨重的工作。所以我建议你使用序列化。希望我的回答对你有所帮助。