Question save xml string passed as string to webservice method in a file

RadhikaChandra

New member
Joined
Apr 25, 2018
Messages
4
Programming Experience
1-3
I want to create a web service method which will taken xml string as input parameter and when I run the webservice I pass the xml string . The webmethod should store the xml string in a file. How can I do in C# using visual studio. please help

for example when I run the below code it works as I have my xml string in the code,

[WebMethod]
public string savexml( )
strConnectionString = new SqlConnection(constring);
try
{
strConnectionString.Open();
XmlDocument doc = new XmlDocument();
doc.LoadXml("<customer><cust> asdadsa </cust></customer>");
using (XmlTextWriter writer = new XmlTextWriter(@"Q:\data1.xml", null))
{
writer.Formatting = Formatting.Indented;
doc.PreserveWhitespace = true;
doc.Save(@"Q:\data1.xml");
}
strConnectionString.Close();
return "file created";
}

but if have to pass the string as parameter as below
public string savexml(string xmlstr )

and change the line in the above code as
doc.LoadXml("xmlstr");
it does not work

What to do?????

 
Think about it. What does this actually do:
doc.LoadXml("xmlstr");

What would be wrong with doing this:
"doc".LoadXml("xmlstr");
 
I also tried

public string savingxmltxt()
{
try {

string unformattedXml = "<?xml version="1.0"?><customer><cust>sdfdsfs</cust></customer>"; string formattedXml = XElement.Parse(unformattedXml).ToString();

XElement.Parse(unformattedXml).Save(@"Q:\doc.xml"); return "file created";
}


catch (Exception ex) { return ex.Message; }
}

 
Being new doesn't mean that you can't think through a problem. Learning how to solve problems for yourself is part of learning how to code. Having someone just give you the solution doesn't help you fix the next problem.

What do you think is wrong with the second line of code I posted compared to the first? Let's change it up a bit. You said that this works:
doc.LoadXml("<customer><cust> asdadsa </cust></customer>");

Do you think that this would work and, if not, why not:
"doc".LoadXml("<customer><cust> asdadsa </cust></customer>");
 
the second point u mentined "doc" will not work as it is referencing to the xmldocument ...u cannot put it in quotes as it will become string
the first line works when it is within the code. but if I have to pass xml string as parameter and read the string in loadxml() it does not work
 
have to pass the string as parameter as below
doc.LoadXml("xmlstr");
it does not work
u cannot put it in quotes as it will become string
That is the lesson here, have you put something in quotes that you shouldn't?
 
the second point u mentined "doc" will not work as it is referencing to the xmldocument ...u cannot put it in quotes as it will become string

That's exactly right, so what's going on here:
doc.LoadXml("xmlstr");

Consider this. What would you expect this code to do:
MessageBox.Show("Hello World");

What would you expect this code to do:
string message = "Hello World";

MessageBox.Show("message");
 
Back
Top Bottom