Jump to content

XML Question

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
i have next XML code


<?xml version="1.0" encoding="utf-8" ?>

<Ime>

  <Tehničar_za_računalstvo>

    <Ime> </Ime>

    <Prezime> </Prezime>

    <Datum_Rođenja> </Datum_Rođenja>

    <Učeni_Jezik> </Učeni_Jezik>

    <Religijski_Predmet> </Religijski_Predmet>

    <Dodatni_Bodovi> </Dodatni_Bodovi>

    <Br_Dod_Bodova> </Br_Dod_Bodova>

    <Matematika_7> </Matematika_7>

    <Matematika_8> </Matematika_8>

    <Hrvatski_7> </Hrvatski_7>

    <Hrvatski_8> </Hrvatski_8>

    <Strani_Jezik_7> </Strani_Jezik_7>

    <Strani_Jezik_8> </Strani_Jezik_8>

    <Fizika_7> </Fizika_7>

    <Fizika_8> </Fizika_8>

    <Teh_Kultura_7> </Teh_Kultura_7>

    <Teh_Kultura_8> </Teh_Kultura_8>

    <Prosjek_7> </Prosjek_7>

    <Prosjek_8> </Prosjek_8>

    <Ponavljač> </Ponavljač>

    <Bodovi> </Bodovi>

    

    

  </Tehničar_za_računalstvo>  

</Ime>

what i want is to sum next columns: Matematika_7, Matematika_8, Hrvatski_7, Hrvatski_8, Strani_Jezik_7, Strani_Jezik_8, Fizika_7, Fizika_8, Teh_Kultura_7, Teh_Kultura_8, Fizika_7, Fizika_8, Teh_Kultura_7, Teh_Kultura_8, Prosjek_7, Prosjek_8 i Br_Dod_Bodova and i want of that sum to be placed at column Bodovi

how to do it in xml

and how can i managed xml code for next problem:
if column "Ponavljač" have value "Da" in that row column Ponavljač must have value 1000

#2
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
i was given tips and sites where to find answer but i didn't find...
all i want to do is when i enter all values in all fields except Bodovi and when i press button for new row program should sum specified columns and add the result in column Bodovi...
please write me code...i know that i should have some code but i don't have...please teach me with code...tnx

#3
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
I'm not exactly sure what it is you're asking Tonchi.
Is it that you want to write code in C# that will read the values from the XML document, add them up and stick the sum in the last field?

Are you hoping to do this in pure XML? (You could do this with something like XSL as well, but that would require an XSL Transformer)

But, anyways, if you loaded that XML into an XML Document

XmlDocument xdoc = new XmlDocument();

xdoc.Load(@"\path\to\your\xmlfile.xml"); //Or xdoc.LoadXml("<?xml version="1.0" encoding="utf-8" ?>.........");


then you can access the contents of it like this:

foreach (XmlNode nod in xdoc.SelectNodes("/Ime/Tehničar_za_računalstvo/*")) {

}

or even

foreach (XmlNode node in xdoc["Ime"]["Tehničar_za_računalstvo"].ChildNodes) {

}



then once you're parsing them, you can literally add them:
foreach (XmlNode nod in xdoc.SelectNodes("/Ime/Tehničar_za_računalstvo/*")) {

    int value, sum = 0;

    if (nod.Name == "Bodovi") {

        nod.InnerText = sum.ToString();

        break;

    }

    if (int.TryParse(nod.InnerText.Trim(), out value) {

        sum += value;

    }

}

all ugliness aside, it's verbose enough for you to pick apart.

And, this hasnt been tested, i'm just talking here...

#4
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
after almost 3 months (i didn't know it was so long) i'm here with XML question AGAIN :)
now after i "master" so easy XML i want to learn XSLT which is XML's CSS (correct me if i'm wrong). i was looking for a some tutorial on w3schools.com and i was getting my eye on this...
XML code:


<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Edited by XMLSpy® -->

<catalog>

	<cd>

		<title>Empire Burlesque</title>

		<artist>Bob Dylan</artist>

		<country>USA</country>

		<company>Columbia</company>

		<price>10.90</price>

		<year>1985</year>

	</cd>

	<cd>

		<title>Hide your heart</title>

		<artist>Bonnie Tyler</artist>

		<country>UK</country>

		<company>CBS Records</company>

		<price>9.90</price>

		<year>1988</year>

	</cd>


and this is XMLT code:


<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Edited by XMLSpy® -->

<xsl:stylesheet version="1.0" 


<xsl:template match="/">

  <html>

  <body>

  <h2>My CD Collection</h2>

    <table border="1">

      <tr bgcolor="#9acd32">

        <th>Title</th>

        <th>Artist</th>

      </tr>

      <xsl:for-each select="catalog/cd">

      <tr>

        <td><xsl:value-of select="naslov"/></td>

        <td><xsl:value-of select="artist"/></td>

      </tr>

      </xsl:for-each>

    </table>

  </body>

  </html>

</xsl:template>

</xsl:stylesheet>


and it gives me one table (this stuff gives me XSLT Tryit Editor v1.0) BUT when i want to change something (so i can learn something) like title and Title to naslov and Naslov (translated on croatian) i'm getting nothing...my question is WHY???

Thanks

#5
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
and i copied a part of XML code

#6
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
yea, you're right about XSL. But it isn't just used to style data visually, like into HTML, (and in that regard, you can use XSL and CSS together)

XSL is used to "transform" data, this means you can convert XML into HTML, Plain Text, even XML (say, formatted differently)

XSL is fantastic, and don't let anyone tell you any different. :cool:

#7
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
but why i can't get what i want...if i can chose what ever name for tag i want, isn't that same for XSLT???




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users