简体   繁体   English

如何从xml文件中仅逐行读取带有xml文件中日期和时间解析的文本?

[英]How can i read from xml file only the text line by line with date and time parse from the xml file?

In this code im using InnerText to view only the text from the xml file without any tags. 在此代码中,im使用InnerText仅查看xml文件中的文本,而没有任何标签。 But i want two things to change now: 但是我现在想改变两件事:

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. To parse the text to lines. 将文本解析为行。

  2. To parse and add to each line the date and time. 解析日期和时间并将其添加到每一行。

Here is an example of the xml file i want to parse from: 这是我要从中解析的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 

Instead to put in a string only the text "testing the test" i want it to be format in the textBox1 like: 而不是只在字符串中输入“ testing the test”文本,我希望它在textBox1中采用以下格式:

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

This is a line. 这是一条线。 Next line will be for example: 下一行例如:

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

But now what im getting in the textBox1 is like this: 但是现在我在textBox1中得到的是这样的:

testing the test testing the test was ok 

Some more text 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 

There are a lot of different ways to parse xml document... Using XmlDocument , XmlTextReader s but i prefer to use Linq to xml classes for parsing existing xml documents, but if I have to create my oun xml document and then parse it I prefer to use Serialization. 有很多不同的方式来解析xml文档...使用XmlDocumentXmlTextReader但是我更喜欢使用Linq来解析现有的xml文档的xml类,但是如果我必须创建自己的oun xml文档然后进行解析,我更喜欢使用序列化。 I'm saing than becouse I don't know where did you take that xml document??? 我在说什么,因为我不知道您将XML文档放在哪里??? Did you crate it yourself??? 你自己装箱了吗??? If yes you should use xml serialization to save your own objects to xml and deserialization to read them back. 如果是,则应使用xml序列化将自己的对象保存为xml,然后使用反序列化将其读回。 It is much easier than parse xml document. 它比解析xml文档容易得多。

Here is an easiest example similar to yours question: As I can understand from your xml example... I used this xml fragment: 这是与您的问题类似的最简单示例:从您的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>

You should create Message class with DateTime, SessionId,From,To and Text fields. 您应该使用DateTime,SessionId,From,To和Text字段创建Message类。 Something like: 就像是:

[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] attribute is necessary for compiler to know that your type (Message) can be serialized. 为了使编译器知道您的类型(消息)可以序列化,[Serializable]属性是必需的。 And after creating you class xml structure you can serialize any instance of your Message or eve array of them List<Message> . 在创建了类xml结构之后,您可以序列化Message任何实例或它们的List<Message> eve数组。 You should do next: 接下来,您应该执行以下操作:

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

You will get the next xml document: 您将获得下一个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>

It is quite similar to yours but now if you need to get all information about that message you don't need to pase xml, you just need to deserialize it and get correct instance of your own Message class: 它与您的非常相似,但是现在,如果您需要获取有关该消息的所有信息,则无需插入xml,只需要反序列化它并获取自己的Message类的正确实例即可:

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

When you get your instance of message you can manipulate it like you want and fill that textbox with any information of your message: 当您获得消息实例时,您可以根据需要对其进行操作,并使用消息的任何信息填充该文本框:

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

Textbox text will be the following: 文本框文字如下:

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

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

if you dont like or dont want to use such methodology, or you just need to parse already created by some third person xml, you can use linq to xml syntax. 如果您不喜欢或不想使用这种方法,或者只需要解析某些第三方XML已经创建的方法,则可以使用linq to xml语法。 For example: 例如:

Create XDocument instance: 创建XDocument实例:

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

but sometimes it can throw the exception, so you need to create XmlDocument, load xml as string, and use static XDocument method to parse that string: 但有时它会引发异常,因此您需要创建XmlDocument,将xml作为字符串加载,并使用静态XDocument方法来解析该字符串:

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

Pay attention about XDocument and XmlDocument. 注意有关XDocument和XmlDocument。 These are two different types. 这是两种不同的类型。 Then you can parse your document using linq syntax: 然后,您可以使用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();

And so on for every attribute or element. 对于每个属性或元素,依此类推。 As you can see it is very difficult and bulky work to parse xml "by hands". 如您所见,“手动”解析xml非常困难且繁琐。 So i would recommend you to use Serialization. 因此,我建议您使用序列化。 Hope my answer helped you. 希望我的回答对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM