如何解析类似的XML文件(此文件只是基本输出)以获取特定的字段值。例如,字段名称" len"?
中的值和大小<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="pdml2html.xsl"?>
<!-- You can find pdml2html.xsl in /usr/share/wireshark or at http://anonsvn.wireshark.org/trunk/wireshark/pdml2html.xsl. -->
<pdml version="0" creator="wireshark/1.8.5" time="Sun May 10 00:54:17 2015" capture_file="">
<packet>
<proto name="geninfo" pos="0" showname="General information" size="60">
<field name="num" pos="0" show="3" showname="Number" value="3" size="60"/>
<field name="len" pos="0" show="60" showname="Frame Length" value="3c" size="60"/>
</proto>
</packet>
<packet>
<proto name="geninfo" pos="0" showname="General information" size="95">
<field name="num" pos="0" show="5" showname="Number" value="5" size="95"/>
<field name="len" pos="0" show="95" showname="Frame Length" value="5f" size="95"/>
</proto>
<packet>
答案 0 :(得分:0)
您可以使用序列化来获取所有数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
PDML pdml = new PDML()
{
packet = new List<Packet>(){
new Packet(){
proto = new Proto(){
name = "geninfo",
pos = 0,
showname = "General information",
size= 60,
field = new List<Field>(){
new Field(){
name = "num",
pos= 0,
show = 3,
showname = "Number",
value= "3",
size = 60
},
new Field(){
name= "len",
pos= 0,
show = 60,
showname = "Frame Length",
value = "3c",
size = 60
}
}
}
},
new Packet(){
proto = new Proto(){
name = "geninfo",
pos = 0,
showname = "General information",
size = 95,
field = new List<Field>(){
new Field(){
name= "num",
pos = 0,
show = 5,
showname = "Number",
value = "5",
size = 95
},
new Field(){
name = "len",
pos = 0,
show = 95,
showname = "Frame Length",
value = "5f",
size = 95
}
}
}
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(PDML));
StreamWriter writer = new StreamWriter(FILENAME);
serializer.Serialize(writer, pdml);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(PDML));
XmlTextReader reader = new XmlTextReader(FILENAME);
PDML newPDML = (PDML)xs.Deserialize(reader);
}
}
}
[XmlRoot("pdml")]
public class PDML
{
[XmlElement("packet")]
public List<Packet> packet {get;set;}
}
[XmlRoot("packet")]
public class Packet
{
[XmlElement("proto")]
public Proto proto { get; set; }
}
[XmlRoot("proto")]
public class Proto
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlAttribute("pos")]
public int pos { get; set; }
[XmlAttribute("showname")]
public string showname { get; set; }
[XmlAttribute("size")]
public int size { get; set; }
[XmlElement("field")]
public List<Field> field { get; set; }
}
[XmlRoot("field")]
public class Field
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlAttribute("pos")]
public int pos { get; set; }
[XmlAttribute("show")]
public int show { get; set; }
[XmlAttribute("showname")]
public string showname { get; set; }
[XmlAttribute("value")]
public string value { get; set; }
[XmlAttribute("size")]
public int size { get; set; }
}