CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2009
    Posts
    34

    XmlTextReader inner node ? attributes ?

    Hi,

    i would like to know what would be the best way to read this xml file or if there is a better way to construct it:

    Code:
    <?xml version="1.0"?>
    <programs>
    	<folder id="ProgramName">
    		<FileStatus>1</FileStatus>
    		<FileCount>1</FileCount>
    		<File1_URL>http://www.mywebsite.com/Launcher/ProgramName/w3.exe</File1_Url>
    		<File1_MD5>351C79485DA9692D0B611B9484DDFAB2</File1_MD5>
    		<File1_Size>526558</File1_Size>
    	</folder>
    </programs>
    As you can see i have a main node with ProgramName which is a program name then inside it on the second field i have file count which is the amount of files to be update then i have the first file which consists of 3 fields, file url, file md5 and file size. Each node can have a different count for example:
    Code:
    <?xml version="1.0"?>
    <programs>
    	<folder id="ProgramName">
    		<FileStatus>1</FileStatus>
    		<FileCount>3</FileCount>
    		<File1_URL>http://www.mywebsite.com/Launcher/ProgramName/w3.exe</File1_Url>
    		<File1_MD5>351C79485DA9692D0B611B9484DDFAB2</File1_MD5>
    		<File1_Size>526558</File1_Size>
    		<File2_URL>http://www.mywebsite.com/Launcher/ProgramName/w3.exe</File1_Url>
    		<File2_MD5>351C79485DA9692D0B611B9484DDFAB2</File1_MD5>
    		<File2_Size>526558</File1_Size>
    		<File3_URL>http://www.mywebsite.com/Launcher/ProgramName/w3.exe</File1_Url>
    		<File3_MD5>351C79485DA9692D0B611B9484DDFAB2</File1_MD5>
    		<File3_Size>526558</File1_Size>
    	</folder>
    	<folder id="ProgramName2">
    		<FileStatus>1</FileStatus>
    		<FileCount>2</FileCount>
    		<File1_URL>http://www.mywebsite.com/Launcher/ProgramName/w3.exe</File1_Url>
    		<File1_MD5>351C79485DA9692D0B611B9484DDFAB2</File1_MD5>
    		<File1_Size>526558</File1_Size>
    		<File2_URL>http://www.mywebsite.com/Launcher/ProgramName/w3.exe</File1_Url>
    		<File2_MD5>351C79485DA9692D0B611B9484DDFAB2</File1_MD5>
    		<File2_Size>526558</File1_Size>
    	</folder>
    	<folder id="ProgramName3">
    		<FileStatus>1</FileStatus>
    		<FileCount>1</FileCount>
    		<File1_URL>http://www.mywebsite.com/Launcher/ProgramName/w3.exe</File1_Url>
    		<File1_MD5>351C79485DA9692D0B611B9484DDFAB2</File1_MD5>
    		<File1_Size>526558</File1_Size>
    	</folder>
    </programs>
    So here is how i was writting the reader until i got locked on how to approch it:

    Code:
                XmlTextReader textReader = new XmlTextReader(Application.StartupPath + "\\Data\\program.xml");
                textReader.Read();
    
                while (textReader.Read())
                {
                    textReader.MoveToElement();
                    if (textReader.FileStatus)
                    {
    			//here is where i am not sure on how to read the 3 sets of lines then i should compare MD5 line with the File we already have in the computer and if it does not match it will add the data to a list for download.
    			if (CalculateChecksum(textReader.id, System.IO.Path.GetFileName(textReader.FileX_URL)) != textReader.FileListMD5)
    			{
    				add fileurl and size to a list
    			}
                    }
                }
    Then i started thinking that there might be a better way to approch this xml file like having inner nodes but so far i havent found anything on inner nodes ...

  2. #2
    Join Date
    Dec 2009
    Posts
    34

    Re: XmlTextReader inner node ? attributes ?

    For the time being i resolved my problem with the follow:
    Code:
                XmlDocument doc = new XmlDocument();
                doc.Load(Application.StartupPath + "\\Data\\program.xml");
                XmlElement root = doc.DocumentElement;
                XmlNodeList nodes = root.SelectNodes("//folder"); 
                foreach (XmlNode node in nodes)
                {
                    if (node["FileStatus"].InnerText == "1")
                    {
                        for (int i = Convert.ToInt32(node["FileCount"].InnerText); i > 0; i--)
                        {
                            if (CalculateChecksum(node["FileFolder"].InnerText, System.IO.Path.GetFileName(node["FileUrl" + i].InnerText)) != node["FileMD5"+i].InnerText)
                            {
                                DownloadList += node["FileUrl" + i].InnerText;
                                totalSize += Int64.Parse(node["FileSize" + i].InnerText);
                                countFiles++;
                            }
                        }
                    }
                }
    Ofcourse i know it is not a good way but i dont have much knowledge on XML so i was trying to put something on pratice so i can understand it better...

    And so i have changed my xml file to:
    Code:
    <?xml version="1.0"?>
    <programs>
    	<folder>
    		<FileFolder>ProgramFolder</FileFolder>
    		<FileStatus>1</FileStatus>
    		<FileCount>3</FileCount>
    		<FileUrl1>http://www.mywebsite.com/Launcher/ProgramFolder/w3.exe</FileUrl1>
    		<FileUrl2>http://www.mywebsite.com/Launcher/ProgramFolder/w2.exe</FileUrl2>
    		<FileUrl3>http://www.mywebsite.com/Launcher/ProgramFolder/w1.exe</FileUrl3>
    		<FileMD51>351C79485DA9692D0B611B9484DDFAB2</FileMD51>
    		<FileMD52>351C79485DA9692D0B611B9484DDFAB2</FileMD52>
    		<FileMD53>351C79485DA9692D0B611B9484DDFAB2</FileMD53>
    		<FileSize1>579674</FileSize1>
    		<FileSize2>579674</FileSize2>
    		<FileSize3>579674</FileSize3>
    	</folder>
    </programs>
    But am sure there is a better way of acomplishing it and if anyone could point me to a direction i would appreciate also if i need to change my xml to any other format let me know, i am sure there is a better way of doing it aswell...
    Last edited by prixone; February 4th, 2010 at 05:53 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured