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. Web Development
  3. ASP.NET
  4. Help with reading XML with C#

Help with reading XML with C#

Scheduled Pinned Locked Moved ASP.NET
csharpxmlhelp
9 Posts 3 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.
  • R Offline
    R Offline
    robert110
    wrote on last edited by
    #1

    Hello everyone: I am struggling with XML (which I am really not good at), the file that I am working with is something like this: <MotherCompany Name="MC">  <Company Name="CompanyOne">     <Department>      <IsPositions>         <Temp>             <MoreNestedDetails></MoreNestedDetails>         </Temp>         <IsPerm>             <MoreNestedDetails></MoreNestedDetails>         </IsPerm>         <Intern>             <MoreNestedDetails></MoreNestedDetails>         </Intern>         <Other>             <MoreNestedDetails></MoreNestedDetails>         </Other>       </IsPositions>     </Department>  </CompanyOne>  <Company Name="CompanyTwo">     <Department>      <IsPositions>         <Temp>             <MoreNestedDetails></MoreNestedDetails>         </Temp>         <IsPerm>             <MoreNestedDetails></MoreNestedDetails>         </IsPerm>         <Intern>             <MoreNestedDetails></MoreNestedDetails>         </Intern>         <Other>             <MoreNestedDetails></MoreNestedDetails>    &n

    N J 2 Replies Last reply
    0
    • R robert110

      Hello everyone: I am struggling with XML (which I am really not good at), the file that I am working with is something like this: <MotherCompany Name="MC">  <Company Name="CompanyOne">     <Department>      <IsPositions>         <Temp>             <MoreNestedDetails></MoreNestedDetails>         </Temp>         <IsPerm>             <MoreNestedDetails></MoreNestedDetails>         </IsPerm>         <Intern>             <MoreNestedDetails></MoreNestedDetails>         </Intern>         <Other>             <MoreNestedDetails></MoreNestedDetails>         </Other>       </IsPositions>     </Department>  </CompanyOne>  <Company Name="CompanyTwo">     <Department>      <IsPositions>         <Temp>             <MoreNestedDetails></MoreNestedDetails>         </Temp>         <IsPerm>             <MoreNestedDetails></MoreNestedDetails>         </IsPerm>         <Intern>             <MoreNestedDetails></MoreNestedDetails>         </Intern>         <Other>             <MoreNestedDetails></MoreNestedDetails>    &n

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Use XPathDocument or XmlDocument instead of the XmlTextReader, unless this is a really huge file. XPathDocument doc = new XPathDocument("MyFile.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodes = nav.Select("//Company"); Now you should have a collection of Company nodes to iterate through


      only two letters away from being an asset

      R 2 Replies Last reply
      0
      • N Not Active

        Use XPathDocument or XmlDocument instead of the XmlTextReader, unless this is a really huge file. XPathDocument doc = new XPathDocument("MyFile.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodes = nav.Select("//Company"); Now you should have a collection of Company nodes to iterate through


        only two letters away from being an asset

        R Offline
        R Offline
        robert110
        wrote on last edited by
        #3

        Mark: Thanks a lot for your suggestion. This document is typically more then 500 lines but the Company Nodes are not the one that increase the file size, its other junk thats embeded in it. Do you think it would be ok to use XPathDocument for such size files? Thanks.

        N 1 Reply Last reply
        0
        • R robert110

          Mark: Thanks a lot for your suggestion. This document is typically more then 500 lines but the Company Nodes are not the one that increase the file size, its other junk thats embeded in it. Do you think it would be ok to use XPathDocument for such size files? Thanks.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          It should be fine as long as the file size isn't more than say 1mb


          only two letters away from being an asset

          R 1 Reply Last reply
          0
          • N Not Active

            It should be fine as long as the file size isn't more than say 1mb


            only two letters away from being an asset

            R Offline
            R Offline
            robert110
            wrote on last edited by
            #5

            Well just came to find out that the file I've been working with is not an xml file instead it is an xsd file:confused: This will explain why xmlreader was working by xpath logic was not working (yeah it was looping thorugh all the elements:-O). Any suggestions?

            1 Reply Last reply
            0
            • R robert110

              Hello everyone: I am struggling with XML (which I am really not good at), the file that I am working with is something like this: <MotherCompany Name="MC">  <Company Name="CompanyOne">     <Department>      <IsPositions>         <Temp>             <MoreNestedDetails></MoreNestedDetails>         </Temp>         <IsPerm>             <MoreNestedDetails></MoreNestedDetails>         </IsPerm>         <Intern>             <MoreNestedDetails></MoreNestedDetails>         </Intern>         <Other>             <MoreNestedDetails></MoreNestedDetails>         </Other>       </IsPositions>     </Department>  </CompanyOne>  <Company Name="CompanyTwo">     <Department>      <IsPositions>         <Temp>             <MoreNestedDetails></MoreNestedDetails>         </Temp>         <IsPerm>             <MoreNestedDetails></MoreNestedDetails>         </IsPerm>         <Intern>             <MoreNestedDetails></MoreNestedDetails>         </Intern>         <Other>             <MoreNestedDetails></MoreNestedDetails>    &n

              J Offline
              J Offline
              Jim Conigliaro
              wrote on last edited by
              #6

              The XmlReader probably isn't the way to go here. Load your xml into an XmlDocument and use the SelectNodes method to select the specific nodes you want, for example you could do something like this: XmlNodeList tempPositions = myXml.SelectNodes("/Company/Department/Temp"); foreach(XmlNode position in tempPositions) tempPositions.Add(position.Name); The best thing you could do is get familiar with XPath queries and use them to extract the specific information you need from your XmlDocument.

              Jim Conigliaro jconigliaro@ieee.org
              http://www.jimconigliaro.com

              1 Reply Last reply
              0
              • N Not Active

                Use XPathDocument or XmlDocument instead of the XmlTextReader, unless this is a really huge file. XPathDocument doc = new XPathDocument("MyFile.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator nodes = nav.Select("//Company"); Now you should have a collection of Company nodes to iterate through


                only two letters away from being an asset

                R Offline
                R Offline
                robert110
                wrote on last edited by
                #7

                XPathDocument doc = new XPathDocument(FileNameWithPath);
                XPathNavigator nav = doc.CreateNavigator();
                XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions");
                //string x = nodes.Current.GetAttribute("Name", nodes.Current.NamespaceURI);
                XPathNavigator nodesNav = nodes.Current;
                XPathNodeIterator ni = nodesNav.SelectDescendants(XPathNodeType.Element, false);
                while(nodes.MoveNext())
                {
                MessageBox.Show(nodes.Current.Name);
                //nodes.Current.GetAttribute("Name",nodes.Current.NamespaceURI);
                // MessageBox.Show(ni.Current.Name + "\n" + ni.Current.Value);
                richTextBox1.AppendText(Environment.NewLine
                + "Name:" + /*x +*/ Environment.NewLine
                + nodes.Current.Name + Environment.NewLine +
                nodes.Current.Value
                );
                }

                When I look in the debug mode to see what is going; this is what I get for my ni Position=0, Current={Root}, Count=0 And aparently nothing happends... Can someone please help me read this document? I sure would appreciate it. Thanks. Regards,Robert

                N 1 Reply Last reply
                0
                • R robert110

                  XPathDocument doc = new XPathDocument(FileNameWithPath);
                  XPathNavigator nav = doc.CreateNavigator();
                  XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions");
                  //string x = nodes.Current.GetAttribute("Name", nodes.Current.NamespaceURI);
                  XPathNavigator nodesNav = nodes.Current;
                  XPathNodeIterator ni = nodesNav.SelectDescendants(XPathNodeType.Element, false);
                  while(nodes.MoveNext())
                  {
                  MessageBox.Show(nodes.Current.Name);
                  //nodes.Current.GetAttribute("Name",nodes.Current.NamespaceURI);
                  // MessageBox.Show(ni.Current.Name + "\n" + ni.Current.Value);
                  richTextBox1.AppendText(Environment.NewLine
                  + "Name:" + /*x +*/ Environment.NewLine
                  + nodes.Current.Name + Environment.NewLine +
                  nodes.Current.Value
                  );
                  }

                  When I look in the debug mode to see what is going; this is what I get for my ni Position=0, Current={Root}, Count=0 And aparently nothing happends... Can someone please help me read this document? I sure would appreciate it. Thanks. Regards,Robert

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #8

                  XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions"); nodes.MoveNext(); XPathNavigator nodesNav = nodes.Current; The iterator is not positioned at the start so you must move to an element before accessing anything.


                  only two letters away from being an asset

                  R 1 Reply Last reply
                  0
                  • N Not Active

                    XPathNodeIterator nodes = nav.Select("/MotherCompany/Compant/Department/IsPositions"); nodes.MoveNext(); XPathNavigator nodesNav = nodes.Current; The iterator is not positioned at the start so you must move to an element before accessing anything.


                    only two letters away from being an asset

                    R Offline
                    R Offline
                    robert110
                    wrote on last edited by
                    #9

                    Thanks for your help and patience with me Mark. However I still dont get any thing even after putting nodes.MoveNext(). Do you think that <xs:schema>, <xs:annotation>, <xs:appinfo> (which are at the top of the document and <MotherCompany> is nested within <xs:appinfo> ) has anything to do with it? Appreciate your help. Regards, Robert

                    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