Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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>

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

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.
Posted
Updated 7-May-13 4:29am
v2
Comments
Sergey Alexandrovich Kryukov 7-May-13 10:42am    
Why? Always start from the root of the problem. Why?
—SA
El Dev 7-May-13 11:58am    
Actually a company will be sending this xml string via my webservice as a post method if I can say that because because data will not be passed in the url.
The scenario is I have give the company my link(a webservice link or url) then what they will do with my link or url is to send the xml string via it, and I think to test if it is working I need to passe the xml string into that textbox.Please if you think this a wrong way please give details on this because I have spending alot of time working on this but no answer.Best Regards!

See a couple of examples:

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

Cheers,
Edo
 
Share this answer
 
Comments
El Dev 7-May-13 10:42am    
There is no other way without using LINQ,I am not good in LinQ.
If possible why not just show the code to add to my Logic.
Joezer BH 7-May-13 10:53am    
Try to clarify your requirements a little better, I am not sure what you need to accomplish and that problem you encountered ...
El Dev 7-May-13 11:02am    
I have a webservice,in that webservice class I have a method that I will invoke it,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
<companyname>Gemeya


Then what I need is to load this xml string as it into xml file that I have into my project.
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.

[EDIT #1]

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. 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[^].


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?

[EDIT #2]

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


  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[^].




Your approach to asking questions is pretty bad. You always need to explain your ultimate goals. There is a very little interest in helping to do something weird, without knowing if it makes sense of not. Try to isolate your goals from your present understanding of how you want to achieve them. If your understanding is wrong, you still have a chance to get a good advice. Only if you explain your goals.

—SA
 
Share this answer
 
v4
Comments
El Dev 7-May-13 10:45am    
Ok Please can you suggest me a better way,If I am entering an xml string into a textbox.How can I load it?
Sergey Alexandrovich Kryukov 7-May-13 10:52am    
A way of what? Please see my comment to the question. Why? What do you want to achieve?
—SA
El Dev 7-May-13 11:34am    
I am getting trouble by adding tag while replying to your question but I have ask down the same question.Please let me know if you got it or not.
Sergey Alexandrovich Kryukov 7-May-13 12:06pm    
Got what? Why? No, I did not. What you say is not a root reason.
After all, I provided you will every link you may want to use to work with XML.
—SA
Sergey Alexandrovich Kryukov 7-May-13 11:30am    
Why?
—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.
 
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