Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. problem with XML

problem with XML

Scheduled Pinned Locked Moved C#
helpquestionalgorithmsxml
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J J4amieC

    Try validating your XML in explorer/any other Xml aware application and you will see its badly formed. You are missing ? marks in the xml declaration line... its should read: < ? xml version="1.0" encoding="utf-8" ? >

    A Offline
    A Offline
    Ahmed Galal
    wrote on last edited by
    #4

    the xml is well formed, about the question mark "?" i just removed them when i did the copy and patse here, i think itsn't the problem, thanx for ur reply

    1 Reply Last reply
    0
    • L leppie

      Xpath can be tricky :p Try some variations eg /VoteData/Question[@id='0']/ /VoteData/Question[@id='0']/* VoteData/Question[@id='0'] I cant recall the exact syntax, but something normally is just in the wrong place :p Also, why not call SelectNodes from the VoteData xml doc itself? that mite be causing the Xpath not to match the VoteData bit. top secret
      Download xacc-ide 0.0.3 now!
      See some screenshots

      A Offline
      A Offline
      Ahmed Galal
      wrote on last edited by
      #5

      i tried all this variations b4 i post this problem, but i'd like to ask somthin, why its tricky ? no standard ? thanx for reply :)

      L 1 Reply Last reply
      0
      • A Ahmed Galal

        i tried all this variations b4 i post this problem, but i'd like to ask somthin, why its tricky ? no standard ? thanx for reply :)

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #6

        Its tricky for me as I rarely use it, and tend to forget the exactly syntax. XPath is simply Regular Expressions for XML. The is a complete standard for it. .NET supports version 1.1 I think. The easiest way to test things is just to start with "*" and continue up to "*/*/*" and see what is really being matched, from there is should be easy, except if you are using XmlNamespaces (which u are not). Your problem looks like a context problem, iow you are starting at the wrong node, like I mentioned in my previous post. top secret
        Download xacc-ide 0.0.3 now!
        See some screenshots

        1 Reply Last reply
        0
        • A Ahmed Galal

          i don't know wheres the error but i'm tired of searching so i'll let u to explore it, my xml file is like the following.

          <VoteData>
              <Question id="0" name="do you believe in think tanks?">
                  <Choice choiceid="1" votes="0" name="Yes">
                      <Vote ip="192.168.1.1"></Vote>
                  </Choice>
                  <Choice choiceid="2" votes="0" name="No">
                  </Choice>
              </Question>
          </VoteData></code>
          

          i'm trying to use SelectNodes() method to select nodes, `XmlDocument VoteData = new XmlDocument(); VoteData.Load(@"C:\VoteData.xml"); XmlElement xElement = VoteData.DocumentElement; XmlNodeList xList = xElement.SelectNodes("/VoteData/Question[@id='0']");` the problem that xList is always empty, no results ! thanx in advance

          M Offline
          M Offline
          MyThread
          wrote on last edited by
          #7

          Why do you have the in you xml file? Check if xElement is empty I use something like this and see if sId has some value XmlNode node; node = xml.FirstChild; while (node.Name != "Ticket" & !(node == null)) { node = node.NextSibling; } string sId; sId = node.Attributes("id").Value;

          M L 2 Replies Last reply
          0
          • M MyThread

            Why do you have the in you xml file? Check if xElement is empty I use something like this and see if sId has some value XmlNode node; node = xml.FirstChild; while (node.Name != "Ticket" & !(node == null)) { node = node.NextSibling; } string sId; sId = node.Attributes("id").Value;

            M Offline
            M Offline
            MyThread
            wrote on last edited by
            #8

            while (node.Name != "Question" & !(node == null)) { node = node.NextSibling; }

            1 Reply Last reply
            0
            • M MyThread

              Why do you have the in you xml file? Check if xElement is empty I use something like this and see if sId has some value XmlNode node; node = xml.FirstChild; while (node.Name != "Ticket" & !(node == null)) { node = node.NextSibling; } string sId; sId = node.Attributes("id").Value;

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #9

              This type of code is exactly what XPath prevents you from having to do :p top secret
              Download xacc-ide 0.0.3 now!
              See some screenshots

              A 1 Reply Last reply
              0
              • A Ahmed Galal

                i don't know wheres the error but i'm tired of searching so i'll let u to explore it, my xml file is like the following.

                <VoteData>
                    <Question id="0" name="do you believe in think tanks?">
                        <Choice choiceid="1" votes="0" name="Yes">
                            <Vote ip="192.168.1.1"></Vote>
                        </Choice>
                        <Choice choiceid="2" votes="0" name="No">
                        </Choice>
                    </Question>
                </VoteData></code>
                

                i'm trying to use SelectNodes() method to select nodes, `XmlDocument VoteData = new XmlDocument(); VoteData.Load(@"C:\VoteData.xml"); XmlElement xElement = VoteData.DocumentElement; XmlNodeList xList = xElement.SelectNodes("/VoteData/Question[@id='0']");` the problem that xList is always empty, no results ! thanx in advance

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #10

                The XPath is fine, that is not where your problem lies. I just made your XML valid and tested it. If I have trouble with an XPath, I use XSLerator or something similar to test it, and build it one step at a time. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                A 1 Reply Last reply
                0
                • C Christian Graus

                  The XPath is fine, that is not where your problem lies. I just made your XML valid and tested it. If I have trouble with an XPath, I use XSLerator or something similar to test it, and build it one step at a time. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

                  A Offline
                  A Offline
                  Ahmed Galal
                  wrote on last edited by
                  #11

                  i think i caught where the problem lies, the problem is me, damn, sorry guys, i didn't paste the exactly code as in my project , its my first time using xml so don't blame me, i think i'm using a namespace, and heres my exactly xml file

                  <?xml version="1.0" encoding="utf-8"?>
                  <VoteData xmlns="http://tempuri.org/VoteData.xsd">
                    <Question id="0" name="do you believe in think tanks?">
                      <Choice choiceid="1" votes="7" name="Yes">
                        <Vote ip="192.168.1.1" />
                        <Vote IP="50.50.20.10" xmlns="" />
                        <Vote IP="50.50.20.10" xmlns="" />
                      </Choice>
                      <Choice choiceid="2" votes="1" name="No">
                        <Vote IP="50.50.20.10" xmlns="" />
                      </Choice>
                    </Question>
                    <Question id="2" name="bet7eb Sawsan?">
                      <Choice choiceid="0" votes="0" name="aiwa" />
                      <Choice choiceid="1" votes="0" name="la2" />
                      <Choice choiceid="2" votes="0" name="moot" />
                      <Choice choiceid="3" votes="0" name="ya3ney" />
                    </Question>
                  </VoteData>
                  

                  i didn't think that will be a diffrent while using namespace, so now, what should i change in my C# code to let XPath work ? sorry guys again and thanx :)

                  1 Reply Last reply
                  0
                  • L leppie

                    This type of code is exactly what XPath prevents you from having to do :p top secret
                    Download xacc-ide 0.0.3 now!
                    See some screenshots

                    A Offline
                    A Offline
                    Ahmed Galal
                    wrote on last edited by
                    #12

                    the file may be large and that way may take a long time to select the node i need, do u think that XPath using this way to select nodes ? i dunno

                    L 1 Reply Last reply
                    0
                    • A Ahmed Galal

                      the file may be large and that way may take a long time to select the node i need, do u think that XPath using this way to select nodes ? i dunno

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #13

                      If speed becomes an issue, look into storing the data in database. top secret
                      Download xacc-ide 0.0.3 now!
                      See some screenshots

                      A 1 Reply Last reply
                      0
                      • L leppie

                        If speed becomes an issue, look into storing the data in database. top secret
                        Download xacc-ide 0.0.3 now!
                        See some screenshots

                        A Offline
                        A Offline
                        Ahmed Galal
                        wrote on last edited by
                        #14

                        yea thats right, but am implementing a stand alone component that don't relies on database, it has its own data file, thanks

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups