拆分一个xml并将元素从其中添加到另一个xml [英] Splitting an xml and adding elements from it to another xml

查看:59
本文介绍了拆分一个xml并将元素从其中添加到另一个xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我是编程新手,正在寻找解决当前问题的方法.

我已经通过C#创建了一个xml,并且我已经在其中保存了所有元素.

现在,我想拆分此xml并将其元素添加到另一个xml中.

我的xml看起来像这样:

人>

-属性>
  身高-> 184cm-
    眼睛->绿色-
     头发->金发-
       年龄-> 24
        名-> Emma
          姓氏->史文森
            快乐->永远快乐
               AngryNode-> HulkSMASH
    /属性>
/Person> 


现有的xml称为"xmldoc",我现在创建了一个名为newxmlDoc的新XML,我想
从旧元素中分离出firstname元素,然后将该信息放入新元素中.

我只创建了一个新的xml文档,不知道从那里去哪里,所以我不觉得显示任何形式的代码都可以帮助解释.

我已经在整个互联网上进行了搜索,因此我需要一个解决方案,而不是5-10链接到可能有所帮助的页面.我也看到了Slavas的链接,但对我没有帮助,因为我在这里做的事情比他所做的要简单得多.

感谢您的帮助!

问候
彼得!



所有这些都在一个简单的控制台应用程序中完成.对于那些可能想要查看代码的人:

 静态  void  Main(字符串 []参数)
        {



            XmlTextWriter objWriter =  XmlTextWriter( @" );

            XmlDocument xmlDoc =  XmlDocument();

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

            xmlDoc.InsertBefore(xmlDeclaration,xmlDoc.DocumentElement);

            xmlDoc.AppendChild(RootElement);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            xmlDoc.Save( @" );
} 


在此之后我要做的就是创建一个新的xml文档,现在我想要上面的xml文档中的信息,并将该信息添加到新的xml中.


为了澄清我不希望从现有xml中获取元素,我希望将其添加到新xml中并从旧xml中删除.

解决方案

为什么不希望您的XML看起来像这样????

 <  人员 > ; 
  <  属性 > 
    <  高度 >  184cm <  /height  > 
    <  眼睛 > 绿色<  /eyes  > 
    <  头发 > 金发<  /hair  > 
    <  年龄 >  24 <  /age  > 
    <  名字 >  Emma <  /firstname  > 
    <  姓氏 >  Svensson <  /lastname  > 
    <  快乐 >  ImAlwaysHappy <  /happy  > 
    <   angrynode  >  HulkSMASH <  /angrynode  > 
  <  /属性 > 
<  /person  >  



然后使用 XMLReader 读取XML,以获取所需的属性.


我认为这会对您有所帮助

使用XPath和XmlDocument(C#)操作XML数据 [ ^ ]

提出问题之前,请先在搜索引擎上进行搜索.


嗨!

昨天不是最成功的一天,我感到沮丧,我的问题成为我沮丧的受害者,对此我表示歉意.我昨天在尝试这个问题几个小时,然后在互联网上搜索答案.

我发现了:
http://stackoverflow.com/questions/184486/what-is-an-intuitive-way-to-move-an-xmlnode-from-one-xmldocument-to-another

这基本上是我的问题的解决方案,或者应该是,但是我无法使它正常工作.

为了澄清我的问题:

我需要知道如何通过C#拆分" xml.我想从我的xml中提取一个节点:

 <?  xml    版本  ="    编码  ="  utf-8" > 
<  人员 > 
  <  属性 > 
    <  高度- >  184cm- <  /Height- > 
    <  眼睛- > 绿色- <  /Eyes- > 
    <  头发- > 金发- <  /头发- > 
    <  年龄- >  24 <  /Age- > 
    <  名字- >  Emma <  /FirstName- > 
    <  姓氏- >  Svensson <  /LastName- > 
    <   Happy- >  ImAlwaysHappy <  /Happy- > 
    <   AngryNode- >  HulkSMASH <  /AngryNode- > 
  <  /属性 > 
<  /人 >   pre> 

并将其放在另一个xml中.所以我的问题是:如何分割xml并将节点从旧的xml添加到新的xml.

我已经通过c#创建了xml:

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);
        }



最后,我创建一个新类的对象,并将创建的xml发送到新类,如下所示:

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");



        }
    }
}



基本上,我应用了我在stackoverflow上找到的代码,这应该是解决我的问题的方法.尽管它没有从旧" xml中剪切元素,但是它只是复制了它,并且当我更改了childnode索引时:

  XmlNode   NodeFromDiffirentDocument   =   sourcdoc . DocumentElement  .ChildNodes [0]; 


到除0以外的任何值,我得到以下错误:

Cannot import a null node.



我已经检查了该论坛,并且我主要是找到可以为我进行拆分的程序以及我在这里不需要的其他更高级的代码,这应该很简单,但我会在某些地方弄乱了.

我是一名初级系统开发人员,我在夏天获得了这份工作,在那里我学到了很多新的有趣的东西.现在我们正在学习通过C#创建xml的学习过程,而我自己需要解决的任务之一就是这个.由于允许使用论坛,因此我不打算在此处获得快速解答,这是一个学习过程,我需要知道如何描述自己的问题并了解自己所能找到的解决方案.

我很感谢我能提供的任何帮助,对于最近的帖子我深表歉意.

问候
彼得!


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>

-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 :

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.

解决方案

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

<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.


I think this will help you

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

Please search it on search engine before asking question.


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 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 :

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!


这篇关于拆分一个xml并将元素从其中添加到另一个xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆