[英]Read nodes of a xml file in C#
如何將以下xml文件讀入列表:
部分XML文件(data.log)
<ApplicationLogEventObject>
<EventType>Message</EventType>
<DateStamp>10/13/2016 11:15:00 AM</DateStamp>
<ShortDescription>N/A</ShortDescription>
<LongDescription>Sending 'required orders' email.</LongDescription>
</ApplicationLogEventObject>
<ApplicationLogEventObject>
<EventType>Message</EventType>
<DateStamp>10/13/2016 11:15:10 AM</DateStamp>
<ShortDescription>N/A</ShortDescription>
<LongDescription>Branches Not Placed Orders - 1018</LongDescription>
</ApplicationLogEventObject>
<ApplicationLogEventObject>
<EventType>Message</EventType>
<DateStamp>10/13/2016 11:15:10 AM</DateStamp>
<ShortDescription>N/A</ShortDescription>
<LongDescription>Branches Not Placed Orders - 1019</LongDescription>
</ApplicationLogEventObject>
...
這是數據訪問層(DAL):
public List<FLM.DataTypes.ApplicationLogEventObject> Get()
{
try
{
XmlTextReader xmlTextReader = new XmlTextReader(@"C:\data.log");
List<FLM.DataTypes.ApplicationLogEventObject> recordSet = new List<ApplicationLogEventObject>();
xmlTextReader.Read();
while (xmlTextReader.Read())
{
xmlTextReader.MoveToElement();
FLM.DataTypes.ApplicationLogEventObject record = new ApplicationLogEventObject();
record.EventType = xmlTextReader.GetAttribute("EventType").ToString();
record.DateStamp = Convert.ToDateTime(xmlTextReader.GetAttribute("DateStamp"));
record.ShortDescription = xmlTextReader.GetAttribute("ShortDescription").ToString()
record.LongDescription = xmlTextReader.GetAttribute("LongDescription").ToString();
recordSet.Add(record);
}
return recordSet;
}
catch (Exception ex)
{
throw ex;
}
}
以及將保存XML文件中的子元素的數據類型:
public class ApplicationLogEventObject
{
public string EventType { get; set; }
public DateTime DateStamp { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
}
將子節點讀入列表后,我想返回它並將其顯示在DataGridView中。
任何有關此問題的幫助將不勝感激。
您的日志文件不是XML文檔。 由於XML文檔必須只有一個根元素 ,因此它是一系列串聯在一起的XML文檔。 這樣的一系列文件,可以通過讀取XmlReader
通過設置XmlReaderSettings.ConformanceLevel == ConformanceLevel.Fragment
。 這樣做之后,您可以使用XmlSerializer
來通讀文件並分別反序列化每個根元素,如下所示:
static List<ApplicationLogEventObject> ReadEvents(string fileName)
{
return ReadObjects<ApplicationLogEventObject>(fileName);
}
static List<T> ReadObjects<T>(string fileName)
{
var list = new List<T>();
var serializer = new XmlSerializer(typeof(T));
var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
using (var textReader = new StreamReader(fileName))
using (var xmlTextReader = XmlReader.Create(textReader, settings))
{
while (xmlTextReader.Read())
{ // Skip whitespace
if (xmlTextReader.NodeType == XmlNodeType.Element)
{
using (var subReader = xmlTextReader.ReadSubtree())
{
var logEvent = (T)serializer.Deserialize(subReader);
list.Add(logEvent);
}
}
}
}
return list;
}
使用以下版本的ApplicationLogEventObject
:
public class ApplicationLogEventObject
{
public string EventType { get; set; }
[XmlElement("DateStamp")]
public string DateStampString {
get
{
// Replace with culturally invariant desired formatting.
return DateStamp.ToString(CultureInfo.InvariantCulture);
}
set
{
DateStamp = Convert.ToDateTime(value, CultureInfo.InvariantCulture);
}
}
[XmlIgnore]
public DateTime DateStamp { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
}
樣本.Net小提琴 。
筆記:
<DateStamp>
10/13/2016 11:15:00 AM
<DateStamp>
元素值10/13/2016 11:15:00 AM
的日期和時間的XML格式不正確,格式為ISO 8601 。 因此,我引入了一個替代string DateStampString
屬性來手動處理往返於所需格式的轉換,然后用XmlIgnore
標記原始的DateTime
屬性。
當XML不縮進時,使用ReadSubtree()
可防止讀取超出每個根元素末尾的可能性。
根據XmlTextReader
的文檔 :
從.NET Framework 2.0開始,我們建議您改用System.Xml.XmlReader類。
因此,我建議使用XmlReader
替換該類型的使用。
<ApplicationLogEventObject>
的子節點不是元素,而是屬性,因此XmlReader.GetAttribute()
不是用於讀取它們的適當方法。
鑒於您的日志文件未按照ISO 8601格式化其時間,因此至少應確保將其格式化為文化上不變的格式,以便可以在具有不同區域設置的計算機之間交換日志文件。 使用CultureInfo.InvariantCulture
轉換可確保這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.