Reading/Parsing an XML string: code review

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gina_Marano

    Reading/Parsing an XML string: code review

    Hey All,

    There are many ways to skin a dog (I like cats so no cats) in .Net

    I have an XML string in the following format (without linefeeds):

    <remoteDir>
    <dir>MyDir</dir>
    <dir>MyDir2</dir>
    <file>
    <name>tst2_3352 0902.txt</name>
    <size>11277</size>
    <lastModTime m=\"10\" d=\"30\" y=\"2006\" hh=\"10\" mm=\"29\"
    ss=\"0\" />
    </file>
    <file>
    <name>tst3_3643 2046.txt</name>
    <size>10897</size>
    <lastModTime m=\"10\" d=\"30\" y=\"2006\" hh=\"7\" mm=\"51\"
    ss=\"0\" />
    </file>
    </remoteDir>

    i would like to get a list of directories and a list of the files and
    attributes.

    I have come up with the following. Is there a better way?

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml( sFileList);

    XmlNodeList filelst = xmlDoc.GetEleme ntsByTagName("f ile");

    for (int i = 0; i < filelst.Count; i++)
    {
    XmlElement afile = (XmlElement) filelst[i];
    XmlElement afilename =
    (XmlElement)afi le.GetElementsB yTagName("name" )[0];
    XmlElement afilesize =
    (XmlElement)afi le.GetElementsB yTagName("size" )[0];
    System.Console. WriteLine(afile name.FirstChild .Value + " " +
    afilesize.First Child.Value);
    }

    I would do the same thing to get the list of directories.

    Thoughts?

    Thanks much!

    ~Gina~

  • Marc Gravell

    #2
    Re: Reading/Parsing an XML string: code review

    Depends on size; if not too big, then this should be fine; however, for
    large xml you may find XmlReader more efficient - but more complex too.

    Marc


    Comment

    • Martin Honnen

      #3
      Re: Reading/Parsing an XML string: code review

      Gina_Marano wrote:
      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.LoadXml( sFileList);
      You can use several APIs to parse out details from XML. In theory an
      XmlDocument is only necessary if you do not only want to parse the XML
      to read out values but also want to manipulate the XML.
      XPathDocument/XPathNavigator suffices to read out values. It should not
      matter much however for the simple and short XML that you have shown.
      XmlNodeList filelst = xmlDoc.GetEleme ntsByTagName("f ile");
      GetElementsByTa gName in .NET has a buggy implementation. Consider using
      SelectNodes instead (or SelectSingleNod e when you are looking for a
      particular node).
      <http://support.microso ft.com/kb/823928/en-us>

      XmlNodeList filelst = xmlDoc.SelectNo des(@"remoteDir/file");
      for (int i = 0; i < filelst.Count; i++)
      {
      XmlElement afile = (XmlElement) filelst[i];
      foreach (XmlElement afile in filelst) {
      XmlElement afilename =
      (XmlElement)afi le.GetElementsB yTagName("name" )[0];
      Again you could use XPath e.g.
      XmlElement afilename = afile.SelectSin gleNode("name") as XmlElement;
      ..NET also allows e.g.
      XmlElement afilename = afile["name"];
      to access child elements.
      afilename.First Child.Value
      InnerText suffices to read out the text contents e.g.
      afilename.Inner Text


      --

      Martin Honnen --- MVP XML

      Comment

      • Gina_Marano

        #4
        Re: Reading/Parsing an XML string: code review

        Thanks a lot Marc for the review!

        ~Gina~

        Marc Gravell wrote:
        Depends on size; if not too big, then this should be fine; however, for
        large xml you may find XmlReader more efficient - but more complex too.
        >
        Marc

        Comment

        Working...