将xml附加到现有的xml [英] Append xml to the existing one

查看:88
本文介绍了将xml附加到现有的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我正在将xml字符串加载到xml文件中,这意味着我正在文本框中输入这个xml字符串:

Hi!I am loading an xml string into xml file, means I am enterenging in a textbox this xml string:

<COMMAND>
     <TYPE>Books</TYPE>
     <COMPANYNAME>Test</COMPANYNAME>
</COMMAND>



然后保存到xml文件中,但它会覆盖该xml文件中存在的所有内容,甚至是定义类型的代码xml文件(<?xml version =1.0encoding =utf-8?>)。

这是我正在使用的代码:


then it is saving into xml file but it overwriting everything wich exist in that xml file,even the code which define the type of xml file(<?xml version="1.0" encoding="utf-8"?>).
This the code I am using:

[WebMethod]
public string COMMAND(string xml)
{
    #region Load received xml and parse the values
    XmlDocument xdoc = new XmlDocument();
    string xmlpath = Path.Combine(Server.MapPath("~/App_Data"), "Sample.xml");
    xdoc.LoadXml(xml + Environment.NewLine);
    XmlElement parentElement = xdoc.CreateElement("WRAP");
    string TYPE= xdoc["COMMAND"].ChildNodes[0].InnerText;
    string COMPANYNAME = xdoc["COMMAND"].ChildNodes[1].InnerText;
    xdoc.DocumentElement.AppendChild(parentElement);
    xdoc.Save(xmlpath);
  }



请有人帮忙解决这个问题,我希望这段代码能够保留定义xml类型的代码并将xml字符串附加到现有的。


Please can someone help resolve this,I want this code to keep the code which define the type of xml and append xml string to the existing one.

推荐答案

看几个例子:



add-data-to-existing-xml-file-using-linq [ ^ ]

LINQ to XML [ ^ ]



干杯,

Edo
See a couple of examples:

add-data-to-existing-xml-file-using-linq[^]
LINQ to XML[^]

Cheers,
Edo


无论你做什么,如果你将一个有效的非空 XML附加到其他有效的非空 XML,你获得的一些文本总是在所有情况下不是a格式良好的XML 。即使第二个XML不包含您在问题中引用的序言。从XML定义可以看出这一点:它总是只有一个根元素。







您始终可以从字符串加载XML。在.NET FCL中,至少有三种不同的XML解析器。这是我对解析可能性的简短概述:

No matter what you do, if you append one valid non-empty XML to some other valid non-empty XML, you obtain some text which is always, in all cases is not a well-formed XML. Even if the second XML does not contain the prolog you quoted in your question. This is obvious from the XML definition: it always has only one root element.



You can always load XML from string. In .NET FCL, there are at least three different XML parsers. This is my short overview of the parsing possibilities:


  1. 使用 System.Xml.XmlDocument 上课。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 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 class 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.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[^].





真正的问题是:为什么从UI加载XML?您真的希望您的用户在某些文本框中输入有效的XML吗?为什么?







要读写XML,您可以使用以下内容选项:





The real question is: why loading XML from UI? Do you really hope that your user will enter valid XML in some text box? Why?



To read and write XML, you can use the following options:



  1. 使用 System.Xml.XmlDocument 上课。它实现了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 ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 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[^].









你提出问题的方法非常糟糕。你总是需要解释你的最终目标。对于帮助做一些奇怪的事情没有什么兴趣,而不知道它是否有意义。尝试将您的目标与目前对如何实现目标的理解隔离开来。如果你的理解是错误的,你仍然有机会得到一个好的建议。只有你解释了你的目标。



-SA


I have a webservice,in that webservice class I have a method that I will invoke,then after invoke it I will pass an xml string into a textbox as you enter value into a textbox.or when trying to add 2 integer using a webservice,same logic
this is the xml string,I am entering into that textbox:
<COMMAND>
     <TYPE>PAYMENT</TYPE>
     <COMPANYNAME>Gemeya</COMPANYNAME>
</COMMAND> 
Then what I need is to load this xml string as it is into xml file that I have into my project.


这篇关于将xml附加到现有的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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