Weird Issue XML AppendChild

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anonymous

    Weird Issue XML AppendChild

    Can you not append a XmlWhiteSpace node to XmlDocument?

    See example:

    using System;
    using System.Xml;

    class TestXml
    {
    public static void Main(string[] args)
    {
    string xmlDoc = @"<root><child> a</child></root>";

    string newXml = @"
    <child>b</child>
    <child>c</child>
    ";

    XmlDocument d = new XmlDocument();
    d.LoadXml(xmlDo c);

    XmlElement tmpNode = d.CreateElement ("newNodes") ;
    tmpNode.InnerXm l = newXml;

    //there are 5 nodes, 2 Elements
    Console.WriteLi ne("Number of nodes: " + tmpNode.ChildNo des.Count);
    int i = 0;
    foreach(XmlNode n in tmpNode)
    {
    //HMM: should run 5 times right since tmpNode has 5 children but only
    runs once!?
    //HOWEVER, if you simply add NON-whitespace nodes, it'll work correctly
    Console.WriteLi ne("NODE: " + i++ + ", " + n);
    //now append the child node
    d.AppendChild(n );
    }

    }
    }


  • Oleg Tkachenko [MVP]

    #2
    Re: Weird Issue XML AppendChild

    anonymous wrote:
    [color=blue]
    > foreach(XmlNode n in tmpNode)[/color]

    Should it be foreach(XmlNode n in tmpNode.ChildNo des) ?

    --
    Oleg Tkachenko [XML MVP]

    Comment

    • anonymous

      #3
      Re: Weird Issue XML AppendChild

      Oleg-
      No difference if you change it.

      And..If you change

      -->foreach(XmlNod e n in tmpNode)

      to:

      -->foreach(XmlNod e n in tmpNode)
      {
      if(n.NodeType != XmlNodeType.Ele ment) continue;

      Still NO GO.

      However, this works:

      -->foreach(XmlNod e n in tmpNode.SelectN odes("//child") )


      Truly baffled... or stupid. Probably the latter.

      "Oleg Tkachenko [MVP]" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
      news:uksHFlLOEH A.3944@tk2msftn gp13.phx.gbl...[color=blue]
      > anonymous wrote:
      >[color=green]
      > > foreach(XmlNode n in tmpNode)[/color]
      >
      > Should it be foreach(XmlNode n in tmpNode.ChildNo des) ?
      >
      > --
      > Oleg Tkachenko [XML MVP]
      > http://blog.tkachenko.com[/color]


      Comment

      • Oleg Tkachenko [MVP]

        #4
        Re: Weird Issue XML AppendChild

        anonymous wrote:
        [color=blue]
        > Oleg-
        > No difference if you change it.
        >
        > And..If you change
        >
        > -->foreach(XmlNod e n in tmpNode)
        >
        > to:
        >
        > -->foreach(XmlNod e n in tmpNode)[/color]

        Hmm, both your strings are equal. I meant that's weird to iterate over a
        single node. Most likely you want to iterate over tmpNode's childrens?

        foreach(XmlNode n in tmpNode.ChildNo des)

        --
        Oleg Tkachenko [XML MVP]

        Comment

        Working...