如何xml文件读取数字和日期时间所有目录和子目录.i有txtnumber和txtdatetime,并搜索所有目录并获取xmldata代表我的search.i想要获取890001000011717.wav
var allfiles = Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
foreach (var item in allfiles)
{
DateTime lastModified = System.IO.File.GetLastWriteTime(item);
string extension;
extension = Path.GetExtension(item);
if (lastModified.ToShortTimeString() == user_time2.ToShortTimeString() && extension == ".xml")
{
XmlReader xmlFile;
xmlFile = XmlReader.Create(item, new XmlReaderSettings());
DataSet dss = new DataSet();
DataView dv;
dss.ReadXml(xmlFile);
string ss = dss.Tables[0].Rows[0]["dataformat"].ToString();
string number = dss.Tables["Party"].Rows[1]["number"].ToString();
}
}
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<recording>
<starttime>2016-05-13 15:03:14:000 +0100</starttime>
<endtime>2016-05-13 15:04:59:000 +0100</endtime>
<calldirection>Incoming</calldirection>
<filename>890001000011717.wav</filename>
<recordingowners>
<recordingowner>202</recordingowner>
</recordingowners>
<parties>
<party id="1">
<number>0711111111</number>
<pstarttime>2016-05-13 15:05:00:703 +0100</pstarttime>
<pendtime>2016-05-13 15:05:00:703 +0100</pendtime>
</party>
</parties>
</recording>
答案 0 :(得分:1)
您可以通过将XML文件反序列化为C#对象然后使用它来轻松完成此操作。
首先,您需要创建XML将反序列化的类(您可以使用Visual Studio的“编辑”菜单轻松完成此操作 - &gt;选择性粘贴 - >将XML粘贴为类):
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class recording
{
public string starttime { get; set; }
public string endtime { get; set; }
public string calldirection { get; set; }
public string filename { get; set; }
[XmlArray]
[XmlArrayItem("recordingowner", IsNullable = false)]
public byte[] recordingowners { get; set; }
[XmlArrayItem("party", IsNullable = false)]
public recordingParty[] parties { get; set; }
}
[XmlType(AnonymousType = true)]
public class recordingParty
{
public string number { get; set; }
public string pstarttime { get; set; }
public string pendtime { get; set; }
[XmlAttribute]
public int id { get; set; }
}
然后使用*.xml
而不是*.*
仅列出xml文件并对其进行反序列化,然后像访问任何其他C#对象一样访问所需的属性:
var allfiles = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
var serializer = new XmlSerializer(typeof(recording));
foreach (var item in allfiles)
{
var lastModified = File.GetLastWriteTime(item);
if (lastModified.ToShortTimeString() != user_time2.ToShortTimeString()) continue;
recording recording;
using (var reader = new StreamReader(item))
{
recording = (recording) serializer.Deserialize(reader);
}
string number = recording.parties[0].number;
}