Read xml file ready

0

Hey guys, I've tried to read an xml file in my program, but there's a line I can not 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>
    
asked by anonymous 28.04.2018 / 22:11

2 answers

1

Greetings.

What you are trying to get is the attribute of the <PATCHNODE> tag, holding when it says:

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

It would be the content (text) of the <NAME> tag that does not even exist.

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

string fileName = n["PATCHNODE"].Attributes["file"].Value;
    
30.04.2018 / 05:15
0

Access the link site and paste the contents of your Xml there, done this takes the C # classes it generates for you.

Then just load the contents of your xml into a variable, and deserialize the xml as shown 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 typeof (StepList) is, StepList is your root object.

    
30.04.2018 / 18:02