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
  1. Home
  2. General Programming
  3. C#
  4. reading XML

reading XML

Scheduled Pinned Locked Moved C#
xmldatabaseperformancehelptutorial
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.
  • E Ennis Ray Lynch Jr

    Didn't you post this same question two days ago?

    Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
    Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
    Most of this sig is for Google, not ego.

    P Offline
    P Offline
    Planker
    wrote on last edited by
    #3

    no my last question was using xpath, and not making any progress.... I guess I will stop posting on code project... I'm not asking someone to write this for me, I just have something new and is not working was trying to get someone to help point out what i am doing wrong so i can move forward. sorry for reposting a different but similar question.

    E 1 Reply Last reply
    0
    • P Planker

      no my last question was using xpath, and not making any progress.... I guess I will stop posting on code project... I'm not asking someone to write this for me, I just have something new and is not working was trying to get someone to help point out what i am doing wrong so i can move forward. sorry for reposting a different but similar question.

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #4

      No, it is just working with those tools is just an absolute PITA and to help most of us would have to paste it into a project and fiddle unless the error just jumps out at us. I usually just use xsd.exe to create a class from the XML file and then serialize into the class rather than xpath since it is lazier and easier in .NET.

      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
      Most of this sig is for Google, not ego.

      P 1 Reply Last reply
      0
      • P Planker

        i am trying to read from an xml document but am not having any luck getting selectsinglenode to work here is the code i am using XmlDocument xml = new XmlDocument(); xml.Load(str); //str has the path to my XML doc XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); //xmln is always null but xml has my xmldoc loaded in memory string Identifier = xmln.InnerText.ToString(); //code bombs here cuz xmln is null... here is the first few lines of the xml - - {89AEAFFE-E1F8-4786-8AF7-6BE2D991625E} i have 20 XSD schema files that are related to the xml docs i am trying to parse but i'm new to xml and not clear on how to use the xsd's help in either direction would be great!

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #5

        Hi again, The xml was missing some end tags so I added them to test this case. When I added a name alias on xmlns attributes it began to work. Here's the code I used. Note the aliases! string testText = "<?xml version=\"1.0\" encoding=\"utf-16\" ?> <GPO xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:**Alias1**=\"http://www.microsoft.com/GroupPolicy/Settings\"> <Identifier> <Identifier xmlns:**Alias2**=\"http://www.microsoft.com/GroupPolicy/Types\">{89AEAFFE-E1F8-4786-8AF7-6BE2D991625E}</Identifier></Identifier></GPO>"; System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); xml.LoadXml(testText); System.Xml.XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); string Identifier = xmln.InnerText.ToString(); Hope this helps you forward, Mika

        The need to optimize rises from a bad design. My articles[^]

        P 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          No, it is just working with those tools is just an absolute PITA and to help most of us would have to paste it into a project and fiddle unless the error just jumps out at us. I usually just use xsd.exe to create a class from the XML file and then serialize into the class rather than xpath since it is lazier and easier in .NET.

          Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
          Most of this sig is for Google, not ego.

          P Offline
          P Offline
          Planker
          wrote on last edited by
          #6

          i've never used xsd.exe before this project but when i tried to create my own xsd files from the xml it complained about to many nested tables. when i found the MS xsd files i tried to create classes but it is saying something about undefined complexType. if there is a way I can use the MS xsd files then great if not i need to figure out how to parse this xml file the hard way and i know it is going to be a PITA.

          1 Reply Last reply
          0
          • P Planker

            i am trying to read from an xml document but am not having any luck getting selectsinglenode to work here is the code i am using XmlDocument xml = new XmlDocument(); xml.Load(str); //str has the path to my XML doc XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); //xmln is always null but xml has my xmldoc loaded in memory string Identifier = xmln.InnerText.ToString(); //code bombs here cuz xmln is null... here is the first few lines of the xml - - {89AEAFFE-E1F8-4786-8AF7-6BE2D991625E} i have 20 XSD schema files that are related to the xml docs i am trying to parse but i'm new to xml and not clear on how to use the xsd's help in either direction would be great!

            N Offline
            N Offline
            netJP12L
            wrote on last edited by
            #7

            XmlNode root = xmlDocument.DocumentElement; for (int i = 0; i < root.ChildNodes.Count; i++) { XmlNode node = root.ChildNodes[i]; if (node.Name == "NAME") { } }

            1 Reply Last reply
            0
            • P Planker

              i am trying to read from an xml document but am not having any luck getting selectsinglenode to work here is the code i am using XmlDocument xml = new XmlDocument(); xml.Load(str); //str has the path to my XML doc XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); //xmln is always null but xml has my xmldoc loaded in memory string Identifier = xmln.InnerText.ToString(); //code bombs here cuz xmln is null... here is the first few lines of the xml - - {89AEAFFE-E1F8-4786-8AF7-6BE2D991625E} i have 20 XSD schema files that are related to the xml docs i am trying to parse but i'm new to xml and not clear on how to use the xsd's help in either direction would be great!

              S Offline
              S Offline
              sph3rex
              wrote on last edited by
              #8

              add http://www.microsoft.com/GroupPolicy/Settings to a new xml namespace manager and use it with your node select. Something like: XmlNamespaceManager NSM = new XmlNamespaceManager(YOURXMLDOCOBJINSTANCE.NameTable); NSM.AddNamespace("some_random_name_like_abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode whateverNode = YOURXMLDOCOBJINSTANCE.SelectSingleNode("//some_random_name_like_abcd:WhateverElement", NSM); XPath will generally not work without specifying the namespace, when a namespace is defined.

              P 1 Reply Last reply
              0
              • W Wendelius

                Hi again, The xml was missing some end tags so I added them to test this case. When I added a name alias on xmlns attributes it began to work. Here's the code I used. Note the aliases! string testText = "<?xml version=\"1.0\" encoding=\"utf-16\" ?> <GPO xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:**Alias1**=\"http://www.microsoft.com/GroupPolicy/Settings\"> <Identifier> <Identifier xmlns:**Alias2**=\"http://www.microsoft.com/GroupPolicy/Types\">{89AEAFFE-E1F8-4786-8AF7-6BE2D991625E}</Identifier></Identifier></GPO>"; System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); xml.LoadXml(testText); System.Xml.XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); string Identifier = xmln.InnerText.ToString(); Hope this helps you forward, Mika

                The need to optimize rises from a bad design. My articles[^]

                P Offline
                P Offline
                Planker
                wrote on last edited by
                #9

                thanks for the tip but the xml code i don't want to change it is an export that i want to import to a database

                W 1 Reply Last reply
                0
                • P Planker

                  thanks for the tip but the xml code i don't want to change it is an export that i want to import to a database

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #10

                  Ok, then I think you should use namespace managers. Look at the second override of this method:

                  public XmlNode SelectSingleNode(
                  string xpath,
                  XmlNamespaceManager nsmgr
                  )

                  http://msdn.microsoft.com/en-us/library/h0hw012b.aspx[^]

                  The need to optimize rises from a bad design. My articles[^]

                  1 Reply Last reply
                  0
                  • S sph3rex

                    add http://www.microsoft.com/GroupPolicy/Settings to a new xml namespace manager and use it with your node select. Something like: XmlNamespaceManager NSM = new XmlNamespaceManager(YOURXMLDOCOBJINSTANCE.NameTable); NSM.AddNamespace("some_random_name_like_abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode whateverNode = YOURXMLDOCOBJINSTANCE.SelectSingleNode("//some_random_name_like_abcd:WhateverElement", NSM); XPath will generally not work without specifying the namespace, when a namespace is defined.

                    P Offline
                    P Offline
                    Planker
                    wrote on last edited by
                    #11

                    thanks, i just tried this and making some progress now here is what i have to get the text of the whole node and the first child XmlDocument xml = new XmlDocument(); xml.Load(str); XmlNamespaceManager NSM = new XmlNamespaceManager(xml.NameTable); NSM.AddNamespace("abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode xmln = xml.SelectSingleNode("//abcd:Identifier", NSM); if (xmln.HasChildNodes == true) { if (xmln.FirstChild.Name == "Identifier") { this.textBox2.Text = xmln.FirstChild.InnerText.ToString(); } } this.textBox1.Text = xmln.InnerText.ToString(); is there a better way to do this or should i get coding? and thanks to everyone!!!

                    modified on Thursday, November 13, 2008 4:15 PM

                    S 1 Reply Last reply
                    0
                    • P Planker

                      thanks, i just tried this and making some progress now here is what i have to get the text of the whole node and the first child XmlDocument xml = new XmlDocument(); xml.Load(str); XmlNamespaceManager NSM = new XmlNamespaceManager(xml.NameTable); NSM.AddNamespace("abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode xmln = xml.SelectSingleNode("//abcd:Identifier", NSM); if (xmln.HasChildNodes == true) { if (xmln.FirstChild.Name == "Identifier") { this.textBox2.Text = xmln.FirstChild.InnerText.ToString(); } } this.textBox1.Text = xmln.InnerText.ToString(); is there a better way to do this or should i get coding? and thanks to everyone!!!

                      modified on Thursday, November 13, 2008 4:15 PM

                      S Offline
                      S Offline
                      sph3rex
                      wrote on last edited by
                      #12

                      rather use /abcd:Identifier/abcd:Identifier to get only the child nodes labeled "Identifier" of the root "Identifier" node. Use the full power of XPath if you went that way ... w3 has a great tutorial on how to use them "productive" ... http://www.w3schools.com/XPath/xpath_syntax.asp[^]

                      Code? Yeah i love it fried together with a glass of wine.

                      P 1 Reply Last reply
                      0
                      • S sph3rex

                        rather use /abcd:Identifier/abcd:Identifier to get only the child nodes labeled "Identifier" of the root "Identifier" node. Use the full power of XPath if you went that way ... w3 has a great tutorial on how to use them "productive" ... http://www.w3schools.com/XPath/xpath_syntax.asp[^]

                        Code? Yeah i love it fried together with a glass of wine.

                        P Offline
                        P Offline
                        Planker
                        wrote on last edited by
                        #13

                        thanks for the link it has been helpful, I have another question here is the xml i am trying to query ... .....settings I need to read are child nodes here..... Folder Redirection .....settings I need to read are child nodes here..... Registry Is there a way i can query the xsi:type value? if not I can query the name but not sure what the best way to jump up to the settings to read them. this xml file does not always have the same entries so i am checking for all possible cases

                        P 1 Reply Last reply
                        0
                        • P Planker

                          thanks for the link it has been helpful, I have another question here is the xml i am trying to query ... .....settings I need to read are child nodes here..... Folder Redirection .....settings I need to read are child nodes here..... Registry Is there a way i can query the xsi:type value? if not I can query the name but not sure what the best way to jump up to the settings to read them. this xml file does not always have the same entries so i am checking for all possible cases

                          P Offline
                          P Offline
                          Planker
                          wrote on last edited by
                          #14

                          i figured out how to do this below is the code i used, could be cleaner if this was the only thing i was checking in the User node XmlDocument xml = new XmlDocument(); xml.Load(str); XmlNamespaceManager NSM = new XmlNamespaceManager(xml.NameTable); NSM.AddNamespace("abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode xmln6 = xml.SelectSingleNode("//abcd:User", NSM); for (int i = 0; i < xmln6.ChildNodes.Count; i++) { XmlNode Childnode = xmln6.ChildNodes[i]; if (Childnode.Name == "ExtensionData") { string ExtDataTest = Childnode.ChildNodes[0].Attributes[1].Value.ToString(); string test = ExtDataTest.Substring(3, ExtDataTest.Length - 3) } }

                          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