如何插入XML文件 [英] How to insert into XML file

查看:86
本文介绍了如何插入XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的XML代码文件,我想通过使用c#代码插入一个新的图像节点,它有一个带有标题节点标题和超链接(没有结束标记)



This is my XML code file , i want to insert a new image node that have a chiled node Title and Hyperlink(without closing tag) by using c# code

<Piecemaker>
  <Contents>


    <Image Source="contents/piecemaker1.png" Title="Project One">
		<Text><h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Text>
		<Hyperlink URL="www.templatemo.com"/>
	</Image>

		
    <Image Source="contents/piecemaker2.png" Title="Project Two">
      <Text><h1>New hot Features</h1><p>This 3D Piecemake slider is integrated with Work Center template from templatemo.com website. The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Text>
      <Hyperlink URL="http://www.templatemo.com" />
    </Image>



<Image Source="contents/piecemaker2.png" Title="Project Two">
      <Text><h1>New hot Features</h1><p>This 3D Piecemake slider is integrated with Work Center template from templatemo.com website. The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Text>
      <Hyperlink URL="http://www.templatemo.com" Target="_blank" />
    </Image>


<Image Source="contents/piecemaker2.png" Title="Project Two">
      <Text><h1>New hot Features</h1><p>This 3D Piecemake slider is integrated with Work Center template from templatemo.com website. The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Text>
      <Hyperlink URL="http://www.templatemo.com" Target="_blank" />
    </Image>


	<Image Source="contents/piecemaker3.png" Title="Project Three">
      <Text><h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Text>
      <Hyperlink URL="http://www.templatemo.com" Target="_blank" />
    </Image>
<Image Source="contents/piecemaker2.png" Title="Project Two">
      <Text><h1>New hot Features</h1><p>This 3D Piecemake slider is integrated with Work Center template from templatemo.com website. The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Text>
      <Hyperlink URL="http://www.templatemo.com" Target="_blank" />
    </Image>
<Image Source="contents/piecemaker2.png" Title="Project Two">
      <Text><h1>New hot Features</h1><p>This 3D Piecemake slider is integrated with Work Center template from templatemo.com website. The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Text>
      <Hyperlink URL="http://www.templatemo.com" Target="_blank" />
    </Image>
<Image Source="contents/piecemaker2.png" Title="Project Two">
      <Text><h1>New hot Features</h1><p>This 3D Piecemake slider is integrated with Work Center template from templatemo.com website. The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Text>
      <Hyperlink URL="http://www.templatemo.com" Target="_blank" />
    </Image>



	
  </Contents>
  <Settings ImageWidth="960" ImageHeight="360" LoaderColor="0x333333" InnerSideColor="0x222222" SideShadowAlpha="0.8" DropShadowAlpha="0.7" DropShadowDistance="25" DropShadowScale="0.95" DropShadowBlurX="40" DropShadowBlurY="4" MenuDistanceX="20" MenuDistanceY="50" MenuColor1="0x999999" MenuColor2="0x333333" MenuColor3="0xFFFFFF" ControlSize="100" ControlDistance="20" ControlColor1="0x222222" ControlColor2="0xFFFFFF" ControlAlpha="0.8" ControlAlphaOver="0.95" ControlsX="450" ControlsY="280" ControlsAlign="center" TooltipHeight="30" TooltipColor="0x222222" TooltipTextY="5" TooltipTextStyle="P-Italic" TooltipTextColor="0xFFFFFF" TooltipMarginLeft="5" TooltipMarginRight="7" TooltipTextSharpness="50" TooltipTextThickness="-100" InfoWidth="400" Inforemoved="0xFFFFFF" InfoBackgroundAlpha="0.95" InfoMargin="15" InfoSharpness="0" InfoThickness="0" Autoplay="10" FieldOfView="45"></Settings>
  <Transitions>
    <Transition Pieces="8" Time="1.4" Transition="easeInOutBack" Delay="0.06" DepthOffset="500" CubeDistance="40"></Transition>
    <Transition Pieces="6" Time="0.9" Transition="easeInOutBack" Delay="0.1" DepthOffset="500" CubeDistance="40"></Transition>
    <Transition Pieces="7" Time="1.2" Transition="easeInOutCubic" Delay="0.1" DepthOffset="500" CubeDistance="40"></Transition>
    <Transition Pieces="9" Time="1.4" Transition="easeInOutQuart" Delay="0.06" DepthOffset="600" CubeDistance="30"></Transition>
  </Transitions>
</Piecemaker>

推荐答案

You can do it by pasting XML and generating it again with new element or attribute added. This is my short overview of approaches offered by .NET FCL:



You can do it by pasting XML and generating it again with new element or attribute added. This is my short overview of approaches offered by .NET FCL:

  1. Use System.Xml.XmlDocument class.它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextWriter System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类系统.Xml.Linq.XDocument ; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].





As you can see, if your file is not too big (for example, the sample shown in the question is quite small), you can conveniently parse it into the DOM structure, modify it in DOM and save it back. This is probably the simplest way. If you have big files and/or need good performance, you can create and instance of XmlTextWriter and XmlTextReader at the same time and write XML as you read input file, detect where you want to add some node and do it on the fly.



—SA



As you can see, if your file is not too big (for example, the sample shown in the question is quite small), you can conveniently parse it into the DOM structure, modify it in DOM and save it back. This is probably the simplest way. If you have big files and/or need good performance, you can create and instance of XmlTextWriter and XmlTextReader at the same time and write XML as you read input file, detect where you want to add some node and do it on the fly.

—SA


Sir , i don’t understand , i’m a biggener , this xml file for a flash video (.swf) and it get his info from XML file that i had posted .. i want to insert a new image node by using c# code ,i have wrote the c# code



Sir , i don't understand , i'm a biggener , this xml file for a flash video (.swf) and it get his info from XML file that i had posted .. i want to insert a new image node by using c# code ,i have wrote the c# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class AddXML : System.Web.UI.Page
{
    XmlDocument docu = new XmlDocument();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        docu.Load(Server.MapPath("~/piecemaker.xml"));
    }
    protected void addbutt_Click(object sender, EventArgs e)
    {

        XmlElement img = docu.CreateElement("Image");
        img.SetAttribute("Source", "~/contents/'"+FileUpload1.FileName+"'");
        img.SetAttribute("Title", TextBox2.Text);
        XmlNode tex = docu.CreateElement("Text");
        tex.InnerText = "<h1>'" + TextBox3.Text + "'</h1><p>'" + TextBox5.Text + "'</p><p>'" + TextBox6.Text + "'</p><p>'" + TextBox7.Text + "'</p>";
        XmlElement hyp = docu.CreateElement("Hyperlink");
        hyp.SetAttribute("URL", TextBox4.Text);
        img.AppendChild(tex);
        img.AppendChild(hyp);
        docu.Save("piecemaker.xml");
        Label1.Text = "done";
}


这篇关于如何插入XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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