Importing nodes without namespace

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

    #1

    Importing nodes without namespace

    Hi, I'm having trouble with namespaces when importing nodes. I'd like to get
    this output:

    <exampleRoot xmlns="http://mynamespace">
    <row Lsm_Info="ABC12 3" />
    <row Lsm_Info="DEF45 6" />
    </exampleRoot>

    But instead I'm getting:

    <exampleRoot xmlns="http://mynamespace">
    <row Lsm_Info="ABC12 3" xmlns="" />
    <row Lsm_Info="DEF45 6" xmlns="" />
    </exampleRoot>

    This is the code I'm using:

    XmlReader reader = command.Execute XmlReader();

    XmlElement rootNode = xmlDocument.Cre ateElement("exa mpleRoot ",
    http://mynamespace);
    xmlDocument.App endChild(rootNo de);

    reader.MoveToCo ntent();
    XmlNode readerNode = xmlDocument.Rea dNode(reader);

    while (readerNode != null)
    {
    XmlNode importNode = xmlDocument.Imp ortNode(readerN ode, false);

    xmlDocument.Doc umentElement.Ap pendChild(impor tNode);

    readerNode = xmlDocument.Rea dNode(reader);
    }

    How do I get rid of the xmlns=""? Any help is much appreciated!

    Cheers,
    Chris


  • Martin Honnen

    #2
    Re: Importing nodes without namespace



    Christoffer wrote:
    [color=blue]
    > Hi, I'm having trouble with namespaces when importing nodes. I'd like to get
    > this output:
    >
    > <exampleRoot xmlns="http://mynamespace">
    > <row Lsm_Info="ABC12 3" />
    > <row Lsm_Info="DEF45 6" />
    > </exampleRoot>
    >
    > But instead I'm getting:
    >
    > <exampleRoot xmlns="http://mynamespace">
    > <row Lsm_Info="ABC12 3" xmlns="" />
    > <row Lsm_Info="DEF45 6" xmlns="" />
    > </exampleRoot>[/color]

    A node is not changed when it is imported or cloned thus if those row
    elements you are importing are in no namespace then the serialization is
    correct, an element in no namespace which is a child of an element
    declaring a default namespace needs to be serialized with xmlns="" to
    make sure the markup undeclares the namespace.

    If you want to have elements in a different namespace then importNode
    does not help, you need to create the new elements in the desired namespace.




    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

    Comment

    • Christoffer

      #3
      Re: Importing nodes without namespace

      "Martin Honnen" <mahotrash@yaho o.de> wrote in message
      news:%238iSb4p7 FHA.2600@tk2msf tngp13.phx.gbl. ..[color=blue]
      >
      > Christoffer wrote:
      >[color=green]
      >> Hi, I'm having trouble with namespaces when importing nodes. I'd like to
      >> get this output:
      >>
      >> <exampleRoot xmlns="http://mynamespace">
      >> <row Lsm_Info="ABC12 3" />
      >> <row Lsm_Info="DEF45 6" />
      >> </exampleRoot>
      >>
      >> But instead I'm getting:
      >>
      >> <exampleRoot xmlns="http://mynamespace">
      >> <row Lsm_Info="ABC12 3" xmlns="" />
      >> <row Lsm_Info="DEF45 6" xmlns="" />
      >> </exampleRoot>[/color]
      >
      > A node is not changed when it is imported or cloned thus if those row
      > elements you are importing are in no namespace then the serialization is
      > correct, an element in no namespace which is a child of an element
      > declaring a default namespace needs to be serialized with xmlns="" to make
      > sure the markup undeclares the namespace.
      >
      > If you want to have elements in a different namespace then importNode does
      > not help, you need to create the new elements in the desired namespace.[/color]

      So the "row" elements/nodes which I get from the XmlReader have to be
      re-created? I can not simply change their namespace?

      Cheers,
      Chris


      Comment

      • Martin Honnen

        #4
        Re: Importing nodes without namespace



        Christoffer wrote:
        [color=blue]
        > So the "row" elements/nodes which I get from the XmlReader have to be
        > re-created? I can not simply change their namespace?[/color]

        You cannot change the namespaceURI of nodes in the DOM implemented in
        ..NET 1.x and 2.0.
        The W3C DOM Level 3 suggests a method named RenameNode on XML document
        objects but .NET does not implement the W3C DOM Level 3, only level 2
        (and lots of Microsoft extensions like InnerXml/OuterXml).

        --

        Martin Honnen --- MVP XML
        http://JavaScript.FAQTs.com/

        Comment

        • Christoffer

          #5
          Re: Importing nodes without namespace

          "Martin Honnen" <mahotrash@yaho o.de> wrote in message
          news:%23FEeCUq7 FHA.2716@TK2MSF TNGP11.phx.gbl. ..[color=blue]
          >
          > Christoffer wrote:
          >
          > You cannot change the namespaceURI of nodes in the DOM implemented in[/color]

          Basically, it can't be done without loads and loads of work right?

          Is there a way to get the StartElement and EndElement from an XmlElement? In
          that case I could create the xml element and then build a string instead of
          using XmlDocument.

          For example,

          string xmlRows = command.Execute Scalar() as string;
          XmlElement rootNode = xmlDocument.Cre ateElement("exa mpleRoot ",
          "http://mynamespace");
          StringBuilder build = new StringBuilder() ;
          build.Append(ro otNode.StartEle ment);
          build.Append(xm lRows);
          build.Append(ro otNode.EndEleme nt);

          That would, in theory, create the result I'd like to get...

          Cheers,
          Chris


          Comment

          • Martin Honnen

            #6
            Re: Importing nodes without namespace



            Christoffer wrote:
            [color=blue]
            > "Martin Honnen" <mahotrash@yaho o.de> wrote in message
            > news:%23FEeCUq7 FHA.2716@TK2MSF TNGP11.phx.gbl. ..
            >[color=green]
            >>Christoffer wrote:
            >>
            >>You cannot change the namespaceURI of nodes in the DOM implemented in[/color]
            >
            >
            > Basically, it can't be done without loads and loads of work right?[/color]

            It can be done with a few lines of code, as said if you have DOM nodes
            you just need to make sure you create the same nodes with the desired
            namespace(s). As only element nodes and attribute nodes can be in a
            namespace you have to write code for those two node types, for all other
            nodes you can clone/import/copy as usual.
            However you have XmlReader you start with so there it might not make
            sense to first use ReadNode to create DOM nodes and then rework the DOM
            with CreateElement but to implement an extension of XmlTextReader that
            simply changes the namespace(s) as needed.



            --

            Martin Honnen --- MVP XML
            http://JavaScript.FAQTs.com/

            Comment

            Working...