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:
="1.0"="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);
XmlElement HeightNode = xmlDoc4.CreateElement("Height-");
XmlText HeightText = xmlDoc4.CreateTextNode("184cm-");
ParentElement.AppendChild(HeightNode);
HeightNode.AppendChild(HeightText);
XmlElement EyeNode = xmlDoc4.CreateElement("Eyes-");
XmlText EyeColorText = xmlDoc4.CreateTextNode("Green-");
ParentElement.AppendChild(EyeNode);
EyeNode.AppendChild(EyeColorText);
XmlElement HairNode = xmlDoc4.CreateElement("Hairs-");
XmlText HairText = xmlDoc4.CreateTextNode("Blond-");
ParentElement.AppendChild(HairNode);
HairNode.AppendChild(HairText);
XmlElement AgeNode = xmlDoc4.CreateElement("Age-");
XmlText AgeText = xmlDoc4.CreateTextNode("24");
ParentElement.AppendChild(AgeNode);
AgeNode.AppendChild(AgeText);
XmlElement FirstNameNode = xmlDoc4.CreateElement("FirstName-");
XmlText FirstNameText = xmlDoc4.CreateTextNode("Emma");
ParentElement.AppendChild(FirstNameNode);
FirstNameNode.AppendChild(FirstNameText);
XmlElement LastNameNode = xmlDoc4.CreateElement("LastName-");
XmlText LastNametxt = xmlDoc4.CreateTextNode("Svensson");
ParentElement.AppendChild(LastNameNode);
LastNameNode.AppendChild(LastNametxt);
XmlElement HappyNode = xmlDoc4.CreateElement("Happy-");
XmlText HappyText = xmlDoc4.CreateTextNode("ImAlwaysHappy");
ParentElement.AppendChild(HappyNode);
HappyNode.AppendChild(HappyText);
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 :
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!