473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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_33520902.txt</name>
<size>11277</size>
<lastModTime m=\"10\" d=\"30\" y=\"2006\" hh=\"10\" mm=\"29\"
ss=\"0\" />
</file>
<file>
<name>tst3_36432046.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.GetElementsByTagName("file");

for (int i = 0; i < filelst.Count; i++)
{
XmlElement afile = (XmlElement) filelst[i];
XmlElement afilename =
(XmlElement)afile.GetElementsByTagName("name")[0];
XmlElement afilesize =
(XmlElement)afile.GetElementsByTagName("size")[0];
System.Console.WriteLine(afilename.FirstChild.Valu e + " " +
afilesize.FirstChild.Value);
}

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

Thoughts?

Thanks much!

~Gina~

Nov 17 '06 #1
3 7951
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
Nov 17 '06 #2
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.GetElementsByTagName("file");
GetElementsByTagName in .NET has a buggy implementation. Consider using
SelectNodes instead (or SelectSingleNode when you are looking for a
particular node).
<http://support.microsoft.com/kb/823928/en-us>

XmlNodeList filelst = xmlDoc.SelectNodes(@"remoteDir/file");
for (int i = 0; i < filelst.Count; i++)
{
XmlElement afile = (XmlElement) filelst[i];
foreach (XmlElement afile in filelst) {
XmlElement afilename =
(XmlElement)afile.GetElementsByTagName("name")[0];
Again you could use XPath e.g.
XmlElement afilename = afile.SelectSingleNode("name") as XmlElement;
..NET also allows e.g.
XmlElement afilename = afile["name"];
to access child elements.
afilename.FirstChild.Value
InnerText suffices to read out the text contents e.g.
afilename.InnerText
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 17 '06 #3
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
Nov 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Volodymyr Sadovyy | last post by:
Hi. Can somebody refer me to resource with specified/analyzed/approximated productivity in Java coding and Java code review tasks? Coding productivity is more described in the net, but I didn't...
3
by: Arvie | last post by:
I need some advice guys.. I am proposing that we get someone to do a complete audit/review of our Java application codebase, about 1000 JSPs/Servlets and 100 EJBs. If I get firms to submit...
0
by: gs-code-review-bounces | last post by:
Your mail to 'gs-code-review' with the subject Re: Application Is being held until the list moderator can review it for approval. The reason it is being held: Post by non-member to a...
6
by: andrew blah | last post by:
Hello I have recently released catchmail - a free (BSD license) open source Python utility www.users.bigpond.net.au/mysite/catchmail.htm This script processes in and outbound emails and stores...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
9
by: Adam Monsen | last post by:
I kindly request a code review. If this is not an appropriate place for my request, where might be? Specific questions are in the QUESTIONS section of the code. ...
3
by: Filippo | last post by:
Hi, In my organization we would like to activate a code review system, in wich a developer have to pass a review from a reviewer before check in the modified files in source safe. Is there any way...
2
by: Jean-Marie Vaneskahian | last post by:
Reading - Parsing Records From An LDAP LDIF File In .Net? I am in need of a .Net class that will allow for the parsing of a LDAP LDIF file. An LDIF file is the standard format for representing...
7
by: Emma Burrows | last post by:
Hello all, I've been writing C# applications and web sites for some time, but I'm now planning to share my latest code with the world at www.codeproject.com. The code works fine, but I'd like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.