XmlNodeReader.Skip Method not working as expected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jambalapamba
    New Member
    • Nov 2007
    • 23

    XmlNodeReader.Skip Method not working as expected

    I am trying to skip element in xml document where style attribute is VISIBILITY: hidden or DISPLAY: none.

    In the following example i want to skip the last two elements but when i skip the element with id=”Div1”
    and next reader.Read() reads “ includeme” but i expected it to read element <a id ="Div2" style="visibili ty:hidden "> and skip it instead its writing “ includeme” to the console

    sample xml
    [code=xml]
    <a id="HyperLink1 " href="jsisland. htm" >NEXT</a>
    <a id="HyperLink2 " href="framset.h tm" class="pop" >gotoframeset </a>

    <div id="Div1" style="DISPLAY: none">
    <a>first</a>
    <a1>second</a>
    </div>

    <a3 id ="Div2" style="VISIBILI TY: hidden "> includeme</a3>
    [/code]

    My code
    Code:
                 XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmldoc);
                XmlNodeReader reader = new XmlNodeReader(xDoc);
    
                while (!reader.EOF)
                {
                  
                        reader.Read();
                        XmlNodeType nodeType = reader.NodeType;
    
                        if (nodeType == XmlNodeType.Element)
                        {
                            if (reader.HasAttributes)
                            {
    
                                for (int i1 = 0; i1 < reader.AttributeCount; i1++)
                                {
                                    reader.MoveToAttribute(i1);
                                     switch (reader.Name)
                                    {
                                        case "style":
                                           if (reader.Value.Contains("VISIBILITY: hidden") || (reader.Value.Contains("DISPLAY: none")))
                                            {
                                                reader.Skip();
                                            }
    
                                            break;
                                    }
                                }
                                
                            }
                        }
                        else if (nodeType == XmlNodeType.Text)
                        {
    
                            Console.WriteLine("other" + reader.Value.ToString()); 
                            Console.ReadLine();
                        }
                      
               } //end while
    Can any one help me with this

    Thank You.
    Last edited by Plater; Jan 2 '08, 03:37 PM. Reason: added [ code ] tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Seems to me your code is doing exactly what it should do.
    You tell it to skip nodes that have visibility:hidd en or Display:none
    So it skips your <div> and <a3> tag.
    And arrives at the text tag "includeme" which it is told to print out.
    Although I would also expect it to print out "other first" and "other second" too. (According to your console.writeli ne() )

    EDIT: On that note, I would think it would print out the whitespace you have in the file too.

    Comment

    • jambalapamba
      New Member
      • Nov 2007
      • 23

      #3
      Originally posted by Plater
      Seems to me your code is doing exactly what it should do.
      You tell it to skip nodes that have visibility:hidd en or Display:none
      So it skips your <div> and <a3> tag.
      And arrives at the text tag "includeme" which it is told to print out.
      Although I would also expect it to print out "other first" and "other second" too. (According to your console.writeli ne() )

      EDIT: On that note, I would think it would print out the whitespace you have in the file too.

      Its printing "other first" and "other second" but i want to skip "includeme" as well because its element value of <a3> tag which i want to skip all the child nodes inside <a3> element.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by jambalapamba
        Its printing "other first" and "other second" but i want to skip "includeme" as well because its element value of <a3> tag which i want to skip all the child nodes inside <a3> element.
        Well that's not the code you have written, you're not skipping the "first" and "second" nodes either, even though they are children of the <div> tag that should be skipped.
        I think you need to go to next sibbling, and not next child. Not sure if "tree logic" is correct here, but it seems like "skip" just moves to the next node, regardless of if it's a child or a sibbling (or even a step back "up")

        Comment

        • jambalapamba
          New Member
          • Nov 2007
          • 23

          #5
          I am really sorry skip method skips entire element my code is not displaying "first" and "second". The skip method skips entire element and its children as well.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Will it skip correctly if you do this with your existing code?
            [code=xml]
            <a3 id ="Div2" style="VISIBILI TY: hidden "><sometag> includeme</sometag></a3>
            [/code]

            Comment

            • jambalapamba
              New Member
              • Nov 2007
              • 23

              #7
              no its not because its not reading <a3> only if it reads a3 then it goes in to the
              nodetype.elemen t loop and skips it, but the problem is its not reading <a3> it is going to the value. Everytime i skip an element its not reading next element tag its reading the value.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Are you going to next AND skip?

                Comment

                • jambalapamba
                  New Member
                  • Nov 2007
                  • 23

                  #9
                  when ever i find style attribute in a element with the condition hidden i will be skipping that entire element and i will be reading next node

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    But if you .Skip() (going to next node) and then .Next(), you are moving twice?

                    Comment

                    • jambalapamba
                      New Member
                      • Nov 2007
                      • 23

                      #11
                      s i am checking that thank you i think thats the mistake i am doing thank you for ur help i am going to change the code and let u know whether it works or not

                      Comment

                      Working...