0

how to xml file read number and date-time all directories and sub directories.i have txtnumber and txtdatetime, and search all directories and get xmldata behalf of my search.i want get 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();
    }
}

The 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

Adeel Khan
  • 185
  • 5
  • 15
  • Recurse the folders and sub folders using http://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c and use XPATH on each https://msdn.microsoft.com/en-us/library/hcebdtae(v=vs.110).aspx – Murray Foxcroft May 26 '16 at 10:18
  • i have txtnumber and txtdatetime, and search all directories and get xmldata behalf of my search. – Adeel Khan May 26 '16 at 10:29

1 Answers1

1

you can do this easily by deserializing the XML file into C# objects then use that.

First you'll need to create the the classes that your XML will deserialize into (you can do this easily using Visual Studio's Edit menu -> Paste Special -> Paste XML As Classes):

[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; }
}

Then list only the xml files using *.xml instead of *.* and deserialize them then access the properties you need just like any other C# object:

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;
}
Nasreddine
  • 36,610
  • 17
  • 75
  • 94