Read xml file ready

Asked

Viewed 289 times

0

Eae guys, I am trying to read an xml file in my program, but it has a line that I am not able to read.

<?xml version="1.0"?>
<XML>
<PATCHINFO>
	<PATCHNODE file="./Unit1.dfm">
		<SIZE>2962</SIZE>
		<CHECKSUM>4206740436</CHECKSUM>
	</PATCHNODE>
	<PATCHNODE file="./Unit1.pas">
		<SIZE>5124</SIZE>
		<CHECKSUM>2933818657</CHECKSUM>
	</PATCHNODE>
	<PATCHNODE file="./atch.pas">
		<SIZE>4286</SIZE>
		<CHECKSUM>1112274213</CHECKSUM>
	</PATCHNODE>
	<PATCHNODE file="./teste.dpr">
		<SIZE>252</SIZE>
		<CHECKSUM>3715331657</CHECKSUM>
	</PATCHNODE>
	<PATCHNODE file="./teste2.res">
		<SIZE>1572</SIZE>
		<CHECKSUM>1544128681</CHECKSUM>
	</PATCHNODE>
</PATCHINFO>
</XML>

My code is edited for xml below

            int curr_array_element = 0;
            Hash hash = new Hash();
            XmlDocument doc = new XmlDocument();
            doc.Load("Patch.xml");
            XmlElement root = doc.DocumentElement;
            XmlNodeList allFiles = root.GetElementsByTagName("PATCHINFO");
            foreach (XmlNode n in allFiles)
            {
                string fileName = n["NAME"].InnerText;
                string fileHash = n["CHECKSUM"].InnerText;
                if (!File.Exists(fileName))
                {
                    this.allSize += Convert.ToInt32(XMLHandler.getSizeOfFile(fileName));
                    this.increaseDownloadNumber();

                    string[] temp = this.toDownloadFile;
                    this.toDownloadFile = new string[this.toDownloadNumber];
                    int curr_store_element = 0;
                    foreach (string curr_element in temp)
                    {
                        this.toDownloadFile[curr_store_element] = curr_element;
                        curr_store_element++;
                    }
                    this.addToDownloadList(fileName, curr_array_element);
                    curr_array_element++;

                }

If I edit xml, in the following way, then I can read

<?xml version="1.0"?>
<XML>
<PATCHINFO>
		<Name>Teste</Name>
		<SIZE>2962</SIZE>
		<CHECKSUM>4206740436</CHECKSUM>
	
	
</PATCHINFO>
</XML>

2 answers

1


Greetings.

What you are trying to get is the tag attribute <PATCHNODE>, so when you say:

string fileName = n["NAME"].InnerText;

It would be the content(text) of the tag <NAME> that doesn’t even exist.

If it was exactly what I understood, you are trying to get the name of the file, which in this case is an attribute of the tag <PATCHNODE>, correct me if I’m wrong. Following my answer is correct:

string fileName = n["PATCHNODE"].Attributes["file"].Value;

0

Access the site http://xmltocsharp.azurewebsites.net/ and paste the contents of your Xml there, done this takes the classes c#, which it generates for you.

Then just load the contents of your xml into a variable, and deserialize the xml as an example below:

string testData = @"<StepList>
                        <Step>
                            <Name>Name1</Name>
                            <Desc>Desc1</Desc>
                        </Step>
                        <Step>
                            <Name>Name2</Name>
                            <Desc>Desc2</Desc>
                        </Step>
                    </StepList>";

XmlSerializer serializer = new XmlSerializer(typeof(StepList));
using (TextReader reader = new StringReader(testData))
{
    StepList result = (StepList) serializer.Deserialize(reader);
}

Remembering that where it says typeof(Steplist), Steplist is your root object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.