2016-05-26 3 views
0

xmlファイルの読み込み回数と日付 - すべてのディレクトリとサブディレクトリにどのようにtxtnumberとtxtdatetimeがあり、すべてのディレクトリを検索してxmldataを検索してget 890001000011717.wavxmlfileのデータを読み取って番号と日付を見つける方法

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> 

enter image description here enter image description here

+0

http://stackoverflow.com/questions/を使用して、フォルダとサブフォルダを再帰的929276/how-to-recursively -list-all-the-c-directory-in-cを使用して、各https://msdn.microsoft.com/en-us/library/hcebdtae(v=vs.110).aspx –

+0

でXPATHを使用します私はtxtnumberとtxtdatetimeを持っていて、すべてのディレクトリを検索し、検索の代わりにxmldataを取得します。 –

答えて

1

ますXMLファイルをC#オブジェクトにデシリアライズしてから簡単に使用できます。

まず、あなたのXMLがにデシリアライズされますクラスを作成する必要があります(あなたはこの簡単に使用してVisual Studioの[編集]メニューを行うことができます - >特殊貼り付け - クラスとして>貼り付け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; 
} 
+0

あなたの返事に感謝します...私の投稿を編集してください –

関連する問題