Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

Im pretty new to programming and im looking for a solution to my curent problem.

Ive made an xml via C# and i have saved all the elements etc in it.

now i want to split this xml and add an element of it in to another xml.

my xml looks like this:

Person>
HTML
-Attributes> 
  Height->184cm- 
    Eyes->Green-
     Hair->Blond- 
       Age->24 
        FirstName->Emma 
          LastName->Svensson 
            Happy->ImAlwaysHappy 
               AngryNode->HulkSMASH 
    /Attributes> 
/Person>

the existing xml is called "xmldoc" and i have now created a new one called newxmlDoc and I want to
split the firstname element from the old one and put that information in to the new one.

I have only crated a new xml doc and don´t know where to go from there so i don´t feel that showing any form of code could help aid this explanation.

I have searched all over the internet so plz I need a solution not 5-10 links to pages that might help. Also i have seen Slavas link and it did not help me since what im doing here feels far less simpler than what he has done.

thankful for your help!

Regards
Peter!


EDIT:
This is all done in one simple console application. for those who might want to see the code :

C#
static void Main(string[] args)
        {



            XmlTextWriter objWriter = new XmlTextWriter(@"c:\temp2\XmlSK.xml", null);

            XmlDocument xmlDoc = new XmlDocument();

            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

            XmlElement RootElement = xmlDoc.CreateElement("Person");

            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);

            xmlDoc.AppendChild(RootElement);

            XmlElement ParentElement = xmlDoc.CreateElement("Attributes");
            xmlDoc.DocumentElement.PrependChild(ParentElement);
            //--- 1
            XmlElement HeightNode = xmlDoc.CreateElement("Height-");
            XmlText HeightText = xmlDoc.CreateTextNode("184cm-");

            ParentElement.AppendChild(HeightNode);
            HeightNode.AppendChild(HeightText);

            //--- 2
            XmlElement EyeNode = xmlDoc.CreateElement("Eyes-");
            XmlText EyeColorText = xmlDoc.CreateTextNode("Green-");

            ParentElement.AppendChild(EyeNode);
            EyeNode.AppendChild(EyeColorText);

            //--- 3
            XmlElement HairNode = xmlDoc.CreateElement("Hair-");
            XmlText HairText = xmlDoc.CreateTextNode("Blond-");

            ParentElement.AppendChild(HairNode);
            HairNode.AppendChild(HairText);

            //--- 4
            XmlElement AgeNode = xmlDoc.CreateElement("Age-");
            XmlText AgeText = xmlDoc.CreateTextNode("24");

            ParentElement.AppendChild(AgeNode);
            AgeNode.AppendChild(AgeText);

            //--- 5
            XmlElement FirstNameNode = xmlDoc.CreateElement("FirstName-");
            XmlText FirstNameText = xmlDoc.CreateTextNode("Emma");

            ParentElement.AppendChild(FirstNameNode);
            FirstNameNode.AppendChild(FirstNameText);

            //--- 6
            XmlElement LastNameNode = xmlDoc.CreateElement("LastName-");
            XmlText LastNametxt = xmlDoc.CreateTextNode("Svensson");

            ParentElement.AppendChild(LastNameNode);
            LastNameNode.AppendChild(LastNametxt);

            //--- 7
            XmlElement HappyNode = xmlDoc.CreateElement("Happy-");
            XmlText HappyText = xmlDoc.CreateTextNode("ImAlwaysHappy");

            ParentElement.AppendChild(HappyNode);
            HappyNode.AppendChild(HappyText);

            //--- 8
            XmlElement AngryNode = xmlDoc.CreateElement("AngryNode-");
            XmlText AngryText = xmlDoc.CreateTextNode("HulkSMASH");

            ParentElement.AppendChild(AngryNode);
            AngryNode.AppendChild(AngryText);

            xmlDoc.Save(@"c:\temp2\XmlSK3.xml");
}

All i have done after this is to create a new xml document and now i want info from the xml document above and add that info to the new xml.

EDIT2:
To clarify I don´t want the the element that i take from the existing xml to be there, i want it to be added to the new xml and removed from the old one.
Posted
Updated 4-Jul-12 3:58am
v5
Comments
Sergey Alexandrovich Kryukov 4-Jul-12 13:03pm    
This is not a question. What kind of help do you expect? What is your problem?
--SA
perilbrain 4-Jul-12 13:28pm    
try pasting your XML first in some textpad like gedit/notepad and then paste it in the question.
Seems like your question has become victim of bad formatting....

Why dont you want your XML to look this way????

XML
<person>
  <attributes> 
    <height>184cm</height>
    <eyes>Green</eyes>
    <hair>Blond</hair> 
    <age>24 </age>
    <firstname>Emma </firstname>
    <lastname>Svensson </lastname>
    <happy>ImAlwaysHappy</happy>
    <angrynode>HulkSMASH</angrynode>
  </attributes> 
</person>


And then reading this XML using XMLReader for the required attributes.
 
Share this answer
 
I think this will help you

Manipulate XML data with XPath and XmlDocument (C#)[^]

Please search it on search engine before asking question.
 
Share this answer
 
Hi!

Yesterday wasn´t the most successful day and i got frustrated and my question became a victim of my frustration and i apologize for that. I was experimenting with this yesterday for a couple of hours and searched the internet for answers.

I found this:
http://stackoverflow.com/questions/184486/what-is-an-intuitive-way-to-move-an-xmlnode-from-one-xmldocument-to-another

which is basically the solution to my question or it should be but i can´t get it to work.

To clarify my question:

I need to know how to "split" an xml via C#. I want to extract a node from my xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<Person>
  <Attributes>
    <Height->184cm-</Height->
    <Eyes->Green-</Eyes->
    <Hairs->Blond-</Hairs->
    <Age->24</Age->
    <FirstName->Emma</FirstName->
    <LastName->Svensson</LastName->
    <Happy->ImAlwaysHappy</Happy->
    <AngryNode->HulkSMASH</AngryNode->
  </Attributes>
</Person>


and put it in another xml. so my question is : How do i split an xml and add an node from the old xml to a new one.

I have created an xml via c# :

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace XmlSkills
{
    class Program
    {
        static void Main(string[] args)
        {



            XmlTextWriter objWriter = new XmlTextWriter(@"c:\temp2\XmlSK.xml", null);

            XmlDocument xmlDoc4 = new XmlDocument();

            XmlDeclaration xmlDeclaration = xmlDoc4.CreateXmlDeclaration("1.0", "utf-8", null);

            XmlElement RootElement = xmlDoc4.CreateElement("Person");

            xmlDoc4.InsertBefore(xmlDeclaration, xmlDoc4.DocumentElement);

            xmlDoc4.AppendChild(RootElement);

            XmlElement ParentElement = xmlDoc4.CreateElement("Attributes");
            xmlDoc4.DocumentElement.PrependChild(ParentElement);
            //--- 1
            XmlElement HeightNode = xmlDoc4.CreateElement("Height-");
            XmlText HeightText = xmlDoc4.CreateTextNode("184cm-");

            ParentElement.AppendChild(HeightNode);
            HeightNode.AppendChild(HeightText);

            //--- 2
            XmlElement EyeNode = xmlDoc4.CreateElement("Eyes-");
            XmlText EyeColorText = xmlDoc4.CreateTextNode("Green-");

            ParentElement.AppendChild(EyeNode);
            EyeNode.AppendChild(EyeColorText);

            //--- 3
            XmlElement HairNode = xmlDoc4.CreateElement("Hairs-");
            XmlText HairText = xmlDoc4.CreateTextNode("Blond-");

            ParentElement.AppendChild(HairNode);
            HairNode.AppendChild(HairText);

            //--- 4
            XmlElement AgeNode = xmlDoc4.CreateElement("Age-");
            XmlText AgeText = xmlDoc4.CreateTextNode("24");

            ParentElement.AppendChild(AgeNode);
            AgeNode.AppendChild(AgeText);

            //--- 5
            XmlElement FirstNameNode = xmlDoc4.CreateElement("FirstName-");
            XmlText FirstNameText = xmlDoc4.CreateTextNode("Emma");

            ParentElement.AppendChild(FirstNameNode);
            FirstNameNode.AppendChild(FirstNameText);

            //--- 6
            XmlElement LastNameNode = xmlDoc4.CreateElement("LastName-");
            XmlText LastNametxt = xmlDoc4.CreateTextNode("Svensson");

            ParentElement.AppendChild(LastNameNode);
            LastNameNode.AppendChild(LastNametxt);

            //--- 7
            XmlElement HappyNode = xmlDoc4.CreateElement("Happy-");
            XmlText HappyText = xmlDoc4.CreateTextNode("ImAlwaysHappy");

            ParentElement.AppendChild(HappyNode);
            HappyNode.AppendChild(HappyText);

            //--- 8
            XmlElement AngryNode = xmlDoc4.CreateElement("AngryNode-");
            XmlText AngryText = xmlDoc4.CreateTextNode("HulkSMASH");

            ParentElement.AppendChild(AngryNode);
            AngryNode.AppendChild(AngryText);

            xmlDoc4.Save(@"c:\temp2\XmlSK4.xml");





            Splitt splitt = new Splitt(xmlDoc4);
        }


At the end i create an object of a new class and send the xml that i have created to the new class, which looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XmlSkills
{
    class Splitt
    {
        public Splitt(XmlDocument xml)
        {
            XmlDocument sourcdoc = xml;
            XmlDocument Xdoc = new XmlDocument();

            XmlDeclaration xmlDeclaration = Xdoc.CreateXmlDeclaration("1.0", "utf-8", null);


            Xdoc.InsertBefore(xmlDeclaration, Xdoc.DocumentElement);

            XmlElement root = Xdoc.CreateElement("Person");
            Xdoc.AppendChild(root);

            XmlElement attributes = Xdoc.CreateElement("Attributes");
            Xdoc.DocumentElement.PrependChild(attributes);

            XmlNode NodeFromDiffirentDocument = sourcdoc.DocumentElement.ChildNodes[0];
            XmlDocument targetDocument = Xdoc;
            XmlNode targetParentNode = Xdoc.DocumentElement;
            bool ShouldDodeeCopy = true;

            XmlNode copyThatBelongsToTargetDocument =
                Xdoc.ImportNode(NodeFromDiffirentDocument, false);
            targetParentNode.AppendChild(copyThatBelongsToTargetDocument);

            Xdoc.Save(@"c:\temp2\XmlNew.xml");



        }
    }
}


Basically i applied the code that i found at stackoverflow which should be the solution to my problem. Although it does not cut the element from the "old" xml, it just copies it and when i changed the childnode index :
CSS
XmlNode NodeFromDiffirentDocument = sourcdoc.DocumentElement.ChildNodes[0];

to anything but 0 i get the following error:
Cannot import a null node.


I have checked the forum and i mostly find programs that do the split for me and other more advanced code that i don´t need here, this should be farily simple but im messing up somwhere.

Im a junior system developer and i got this job during the summer where im learning a ton of new and interesting stuff. Now we are in the learning process of creating xml via C# and one of the tasks that i need to solve by myself is this. Im not trying to get a fast answer here since the help of forums is allowed, it´s a learning process where i need to know how to describe my problem and understand the solution that i get or that i find.

Im thankful for any help that i can get and I apologize for the recent post.

Regards
Peter!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900