How to copy the whole RootElement in an .xml file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raghudr@gmail.com

    How to copy the whole RootElement in an .xml file

    <?xml version="1.0" standalone="yes " ?>
    - <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
    ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
    <Item Name="raj" VALUE="60864"/>
    <Item Name="rag" VALUE="60868" />
    </AddressSpace>


    Hi all,

    I have a .xml file like this.I have to do lot of operations by reading
    comparing and copying some data to other files like that.

    Now my problem i am struck:- I want to copy the full Root element and
    put it in another .xml file

    <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
    ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">

    root element i want to copy

    I can do it this way

    string strfiletype =
    xdoc.DocumentEl ement.GetAttrib ute("xsi:noName spaceSchemaLoca tion".ToString( ));
    string strName =
    xdoc.DocumentEl ement.GetAttrib ute("Name".ToSt ring());
    string strConfigMax =
    xdoc.DocumentEl ement.GetAttrib ute("ConfigMax" .ToString());

    ....

    and then use setattribute to write to a new .xml file.

    But my rootelement keeps chnaging and all the times it is may not
    know the attribute name

    hence i want to copy whole rootelement without using any names and put
    it in a new .xml file.

    please help me on this

    thanks in advance
    RAGHU

  • Martin Honnen

    #2
    Re: How to copy the whole RootElement in an .xml file

    raghudr@gmail.c om wrote:
    <?xml version="1.0" standalone="yes " ?>
    - <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
    ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
    <Item Name="raj" VALUE="60864"/>
    <Item Name="rag" VALUE="60868" />
    </AddressSpace>
    >
    >
    Hi all,
    >
    I have a .xml file like this.I have to do lot of operations by reading
    comparing and copying some data to other files like that.
    >
    Now my problem i am struck:- I want to copy the full Root element and
    put it in another .xml file
    >
    <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
    ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
    You can use ImportNode on the DocumentElement e.g.

    XmlDocument doc1 = new XmlDocument();
    doc1.Load(@"XML File1.xml");

    XmlDocument doc2 = new XmlDocument();
    doc2.AppendChil d(doc2.ImportNo de(doc1.Documen tElement, false));
    doc2.Save("XMLF ile2.xml");


    That way you copy the element and its attributes from doc1 to doc2.




    --

    Martin Honnen --- MVP XML

    Comment

    • Family Tree Mike

      #3
      Re: How to copy the whole RootElement in an .xml file

      I found this trick once to do this (copy docInput DocumentElement to
      docOutput):


      XmlNode n = docOutput.Impor tNode(docInput. DocumentElement , true);
      docOutput.Docum entElement.Appe ndChild(n);

      Hope this helps.

      <raghudr@gmail. comwrote in message
      news:0e4b0ef3-2910-464d-923f-b405f6c0749d@n3 3g2000pri.googl egroups.com...
      <?xml version="1.0" standalone="yes " ?>
      - <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
      ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
      <Item Name="raj" VALUE="60864"/>
      <Item Name="rag" VALUE="60868" />
      </AddressSpace>
      >
      >
      Hi all,
      >
      I have a .xml file like this.I have to do lot of operations by reading
      comparing and copying some data to other files like that.
      >
      Now my problem i am struck:- I want to copy the full Root element and
      put it in another .xml file
      >
      <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
      ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
      >
      root element i want to copy
      >
      I can do it this way
      >
      string strfiletype =
      xdoc.DocumentEl ement.GetAttrib ute("xsi:noName spaceSchemaLoca tion".ToString( ));
      string strName =
      xdoc.DocumentEl ement.GetAttrib ute("Name".ToSt ring());
      string strConfigMax =
      xdoc.DocumentEl ement.GetAttrib ute("ConfigMax" .ToString());
      >
      ...
      >
      and then use setattribute to write to a new .xml file.
      >
      But my rootelement keeps chnaging and all the times it is may not
      know the attribute name
      >
      hence i want to copy whole rootelement without using any names and put
      it in a new .xml file.
      >
      please help me on this
      >
      thanks in advance
      RAGHU
      >

      Comment

      • Family Tree Mike

        #4
        Re: How to copy the whole RootElement in an .xml file

        I just saw Martin's response. The difference in the true/false argument to
        ImportNode is whether the subelements come along for the ride. It was not
        clear if you wanted them or not.

        "Family Tree Mike" <FamilyTreeMike @ThisOldHouse.c omwrote in message
        news:%23KqOHvbP JHA.4576@TK2MSF TNGP03.phx.gbl. ..
        >I found this trick once to do this (copy docInput DocumentElement to
        >docOutput):
        >
        >
        XmlNode n = docOutput.Impor tNode(docInput. DocumentElement , true);
        docOutput.Docum entElement.Appe ndChild(n);
        >
        Hope this helps.
        >
        <raghudr@gmail. comwrote in message
        news:0e4b0ef3-2910-464d-923f-b405f6c0749d@n3 3g2000pri.googl egroups.com...
        ><?xml version="1.0" standalone="yes " ?>
        >- <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
        >ConfigMax="1 " xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
        > <Item Name="raj" VALUE="60864"/>
        > <Item Name="rag" VALUE="60868" />
        ></AddressSpace>
        >>
        >>
        >Hi all,
        >>
        >I have a .xml file like this.I have to do lot of operations by reading
        >comparing and copying some data to other files like that.
        >>
        >Now my problem i am struck:- I want to copy the full Root element and
        >put it in another .xml file
        >>
        ><AddressSpac e xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
        >ConfigMax="1 " xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
        >>
        >root element i want to copy
        >>
        >I can do it this way
        >>
        > string strfiletype =
        >xdoc.DocumentE lement.GetAttri bute("xsi:noNam espaceSchemaLoc ation".ToString ());
        > string strName =
        >xdoc.DocumentE lement.GetAttri bute("Name".ToS tring());
        > string strConfigMax =
        >xdoc.DocumentE lement.GetAttri bute("ConfigMax ".ToString( ));
        >>
        >...
        >>
        >and then use setattribute to write to a new .xml file.
        >>
        >But my rootelement keeps chnaging and all the times it is may not
        >know the attribute name
        >>
        >hence i want to copy whole rootelement without using any names and put
        >it in a new .xml file.
        >>
        >please help me on this
        >>
        >thanks in advance
        >RAGHU
        >>
        >

        Comment

        • raghudr@gmail.com

          #5
          Re: How to copy the whole RootElement in an .xml file

          On Nov 3, 7:03 pm, "Family Tree Mike"
          <FamilyTreeM... @ThisOldHouse.c omwrote:
          I just saw Martin's response.  The difference in the true/false argument to
          ImportNode is whether the subelements come along for the ride.  It was not
          clear if you wanted them or not.
          >
          "Family Tree Mike" <FamilyTreeM... @ThisOldHouse.c omwrote in messagenews:%23 KqOHvbPJHA.4576 @TK2MSFTNGP03.p hx.gbl...
          >
          >
          >
          I found this trick once to do this (copy docInput DocumentElement to
          docOutput):
          >
            XmlNode n = docOutput.Impor tNode(docInput. DocumentElement , true);
            docOutput.Docum entElement.Appe ndChild(n);
          >
          Hope this helps.
          >
          <ragh...@gmail. comwrote in message
          news:0e4b0ef3-2910-464d-923f-b405f6c0749d@n3 3g2000pri.googl egroups.com....
          <?xml version="1.0" standalone="yes " ?>
          - <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
          ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
           <Item Name="raj" VALUE="60864"/>
           <Item Name="rag" VALUE="60868" />
          </AddressSpace>
          >
          Hi all,
          >
          I have a .xml file like this.I have to do lot of operations by reading
          comparing and copying some data to other files like that.
          >
          Now my problem i am struck:- I want to copy the full Root element and
          put it in another .xml file
          >
          <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
          ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
          >
          root element i want to copy
          >
          I can do it this way
          >
                      string strfiletype =
          xdoc.DocumentEl ement.GetAttrib ute("xsi:noName spaceSchemaLoca tion".ToString( ­));
                     string strName =
          xdoc.DocumentEl ement.GetAttrib ute("Name".ToSt ring());
                    string strConfigMax =
          xdoc.DocumentEl ement.GetAttrib ute("ConfigMax" .ToString());
          >
          ...
          >
          and then use setattribute to write to a new .xml file.
          >
          But my rootelement keeps chnaging and all the times it is  may not
          know the attribute name
          >
          hence i want to copy whole rootelement without using any names and put
          it in a new .xml file.
          >
          please help me on this
          >
          thanks in advance
          RAGHU- Hide quoted text -
          >
          - Show quoted text -
          Hi all,

          Thnx all for your reply,i just want to copy only the rooteement and
          write in a new .xml file only the rootelement.

          The other things should not be copied.

          <AddressSpace xsi:noNamespace SchemaLocation= "prince.xsd " Name="U2"
          ConfigMax="1" xmlns:xsi="http ://www.rag.org/2001/XMLSchema-instance">
          only this i will copy and put it in a new .xml file

          can anyone please tell me on this

          Thanks in advance,
          RAGHU

          Comment

          • Martin Honnen

            #6
            Re: How to copy the whole RootElement in an .xml file

            raghudr@gmail.c om wrote:
            Thnx all for your reply,i just want to copy only the rooteement and
            write in a new .xml file only the rootelement.
            I did show you how to do that in



            --

            Martin Honnen --- MVP XML

            Comment

            • raghudr@gmail.com

              #7
              Re: How to copy the whole RootElement in an .xml file

              On Nov 3, 10:58 pm, Martin Honnen <mahotr...@yaho o.dewrote:
              ragh...@gmail.c om wrote:
              Thnx all for your reply,i just want to copy only the rooteement and
              write in a new .xml file only the rootelement.
              >
              I did show you how to do that inhttp://groups.google.c om/group/microsoft.publi c.dotnet.langua ges.csha...
              >
              --
              >
                      Martin Honnen --- MVP XML
                     http://JavaScript.FAQTs.com/
              Hi martin,

              your solution is working, but one problem is there:-

              XmlDocument doc1 = new XmlDocument();
              doc1.Load(@"XML File1.xml");


              XmlDocument doc2 = new XmlDocument();
              doc2.Load(@"new file.xml"); -->(This is the change in my
              code- i have loaded a new file- the new file is a result of lot of
              operations done using the dataset class
              with
              rootelement as only <Addressspace/>.I want to open the new file
              replace only the rootelement line to proper one(full
              l line) and
              then save it.I don't want to touch any nodes.

              doc2.AppendChil d(doc2.ImportNo de(doc1.Documen tElement,
              false)); //if i put this code the newfile .xml file will have only the
              rootelement and nothing else
              doc2.Save("XMLF ile2.xml");

              please help on this

              thanks in advance,
              RAGHU

              Comment

              • Martin Honnen

                #8
                Re: How to copy the whole RootElement in an .xml file

                raghudr@gmail.c om wrote:
                XmlDocument doc1 = new XmlDocument();
                doc1.Load(@"XML File1.xml");
                >
                >
                XmlDocument doc2 = new XmlDocument();
                doc2.Load(@"new file.xml"); -->(This is the change in my
                code- i have loaded a new file- the new file is a result of lot of
                operations done using the dataset class
                with
                rootelement as only <Addressspace/>.I want to open the new file
                replace only the rootelement line to proper one(full
                l line) and
                then save it.I don't want to touch any nodes.
                >
                doc2.AppendChil d(doc2.ImportNo de(doc1.Documen tElement,
                false)); //if i put this code the newfile .xml file will have only the
                rootelement and nothing else
                doc2.Save("XMLF ile2.xml");
                You can't solve that with a single operation. You need to store the
                DocumentElement first e.g.
                XmlNode oldRoot = doc2.DocumentEl ement;
                XmlNode newRoot = doc2.ImportNode (doc1.DocumentE lement, false);
                doc2.ReplaceChi ld(newRoot, oldRoot);
                while (oldRoot.HasChi ldNodes)
                {
                newRoot.AppendC hild(oldRoot.Fi rstChild);
                }
                doc2.Save("XMLF ile2.xml");


                --

                Martin Honnen --- MVP XML

                Comment

                Working...