I need to save the xml response into sesssion. but I have tried this But It didn't . but I Have save xml request as session It worked. I Have attached working and non working code. can any one please help me on this. I don't want save the xml response as file.
Working Code
String xmltest = Session["xmlreq"].ToString();
SoapClient soap = new SoapClient();
string prueba = soap.RequestResponseMethod("getHotelValuedAvail", xmltest);
string tham = HttpUtility.HtmlDecode(prueba);
XmlDocument doc = new XmlDocument();
doc.LoadXml(tham);
doc.Save(Server.MapPath("hotelrs.xml"));
XslTransform myXslTransform;
myXslTransform = new XslTransform();
myXslTransform.Load(Server.MapPath("hotel.xsl"));
myXslTransform.Transform(Server.MapPath("hotelrs.xml"), Server.MapPath("transformhotels.xml"));
Non working Code
String xmltest = Session["xmlreq"].ToString();
SoapClient soap = new SoapClient();
string prueba = soap.RequestResponseMethod("getHotelValuedAvail", xmltest);
string tham = HttpUtility.HtmlDecode(prueba);
Session.Add("xmlrs", tham);
XmlDocument doc = new XmlDocument();
doc.LoadXml(Session["xmlrs"].ToString());
//doc.Save(Server.MapPath("hotelrs.xml"));
XmlDocument trdoc = new XmlDocument();
XslTransform myXslTransform;
myXslTransform = new XslTransform();
myXslTransform.Load(Server.MapPath("hotel.xsl"));
myXslTransform.Transform(doc.InnerXml, trdoc.InnerXml);
Session.Add("xmltrs", trdoc.InnerXml);
1.Declare a variable.
2.store ur xml request data in that variable.
3.Declare another variable say (String Result).
4.Now Result="Call ur method here which will give u a xml response".
5.Session["Outcome"]=Result;
6.No need to use any XMLDocument here.
If u want to format ur XML response, use XSLT template.
In aspx page
<div id="DivLoad">
<asp:Xml ID="xmlDaynamic" runat="server" Visible="true"></asp:Xml>
</div>
In cs
xmlDaynamic.DocumentContent = session["outcome"];
xmlDaynamic.TransformSource = "yourxslttemplate.xslt";
Hope this will help you.
Related
I am trying to update an existing XML file by adding a new child node using c#.
Everything is OK if I save it by new name but I want to update the same file and while doing it, got the following exception:
System.IO.IOException:Process cannot access the file... because it is
being used by another process
Here is my code: (I am trying to add a new default node)
XmlDocument doc = new XmlDocument();
string path = #"C:\Debug\default.xml";
doc.Load(path);
XmlNode NName = doc.CreateElement("default");
XmlNode SNO = doc.CreateElement("SNo");
SNO.InnerText = "2";
NName.AppendChild(SNO);
doc.DocumentElement.AppendChild(NName);
doc.Save(path);
Also XML file:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<default>
<SNo>1</SNo>
</default>
</NewDataSet>
If you are sure the file is only being used by your process, then simply read it into a byte array, close the file, then save it again:
(I am using .net 4.0 for this sample):
XmlDocument doc = new XmlDocument();
byte[] content = File.ReadAllBytes(path);
using (var memStream = new MemoryStream(content))
{
doc.Load(memStream);
}
XmlNode NName = doc.CreateElement("default");
XmlNode SNO = doc.CreateElement("SNo");
SNO.InnerText = "2";
NName.AppendChild(SNO);
doc.DocumentElement.AppendChild(NName);
doc.Save(path);
Ive been using the following code to call up data coming from localhost:8080 in xml format. This part of the code works fine:
private void openP()
{
String sUrl = "http://localhost:8080/GetOpen=";
XmlTextReader rssReader = new XmlTextReader(sUrl.ToString());
XmlDocument rssDoc = new XmlDocument();
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sUrl);
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream, Encoding.UTF8);
WebResponse wr = wrGETURL.GetResponse();
}
I want to know how I can now use the data that xhttp://localhost:8080/GetOpen= returns as a variable in my program.
for example if I were to goto xhttp://localhost:8080/GetOpen= in a web browser I would see this:
<Response>
<Content>
<Position Symbol="xVAR" Market="blah" >
</Content>
</Response>
how would I go about making xVar a String variable that I can just simply write to a label?
Thanks
Since you're using XmlDocument, then you can use SelectSingleNode() method passing correct XPath to get particular part of the XML :
rssDoc.Load("http://localhost:8080/GetOpen=");
.....
XmlNode symbol = rssDoc.SelectSingleNode("//Position/#Symbol");
String symbolValue = "";
if(symbol != null) symbolValue = symbol.Value;
I have a string var that store an xml from a request to a RESTful service.
I have a problem transforming this with an xslt file on a fly without saving it.
I am getting this error
System.UriFormatException: Invalid URI: The Uri scheme is too long. On this line
xslt.Transform(xmldoc, null, writer);
string xmldoc = xReq("http://restful.com/RestAPI");
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(#"C:\Users\XSeXml\xRes.xslt");
string htmlOutput;
StringWriter writer = new StringWriter();
xslt.Transform(xmldoc, null, writer);
htmlOutput = writer.ToString();
Literal1.Text = htmlOutput;
writer.Close();
How to transform XML as a string w/o using files in .NET?
Ideas from the link above helps to overcome the problem by passing the string to the XmlReader before transforming it.
I'm new to serialization.I need to send a Serialized object from a one web site to another web site.
i'm using following serialization code in my first web site
private void Serialize()
{
Cuser cat = new Cuser();
cat.UserNO = 1;
cat.UerName = "chamara";
cat.Passwod = "123";
XmlSerializer ser = new XmlSerializer(cat.GetType());
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
ser.Serialize(writer, cat);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
Response.Redirect("https://site1.com.au");
}
now i need to retrieve(deserialize) these data from site1.com.i have the deserialization code.i need to know how can i transfer this object? and am i using serialization for the correct purpose? hope my explanation is clear enough to understand the issue.
There is some chance that you want to render data in a form on site-1 and automatically post the form to site-2 with client side script.
I wanted to find out if there is a way of getting a parameter or variable value out of an XSL file. For example, if I have the following:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="username" select ="usertest"/>
<xsl:variable name="password" select ="pass"/>
<!-- ... -->
</xsl:stylesheet>
I would like to read the username and password values from the XSL and use them for authentication. I am using ASP.Net and C# to perform the actual transform on an XML file.
Could someone please share code with me that would allow me to read the XSL variables from ASP.NET/C#. Thanks in advance for the help.
This is easy. XSL files are XML themselves, so you can treat them as such.
XmlDocument xslDoc = new XmlDocument();
xslDoc.Load("myfile.xsl");
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xslDoc.NameTable);
nsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
XmlNode usrNode = xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:variable[#name='username']", nsMgr);
XmlNode pwdNode = xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:variable[#name='password']", nsMgr);
string usr = usrNode.Attributes["select"].Value;
string pwd = pwdNode.Attributes["select"].Value;
Your question is (edit: was) missing the actual code, but from the description it appears what you are looking for is XPath. XSL will transform one XML document into another XML document, you can then use XPath to query the resulting XML to get out the values that you want.
This Microsoft KB article has information about how to use XPath from C#:
http://support.microsoft.com/kb/308333
Thanks Everone. Here is what finally worked:
Client (asp with vbscript) Used for Testing Purposes:
<%
//Create Object
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
//Set up the object with the URL
'xmlhttp.open "POST" ,"http://localhost/ASP_Test/receiveXML.asp",False
//Create DOM Object
Set xmldom = CreateObject("Microsoft.XMLDOM")
xmldom.async = false
//Load xls to send over for transform
xmldom.load(Server.MapPath("/ASP_Test/masterdata/test.xsl"))
//Send transform file as DOM object
xmlhttp.send xmldom
%>
//////////////////////////////////////////////////////////////////////////
On the Server Side: (aspx with C#) Accepts xslt and process the transform:
//file path for data xml
String xmlFile = ("\\masterdata\\test.xml");
//file path for transformed xml
String xmlFile2 = ("\\masterdata\\out.xml");
XmlTextReader reader = new XmlTextReader(Request.InputStream);
Transform(xmlFile, reader, xmlFile2);
public static string Transform(string sXmlPath, XmlTextReader xslFileReader, string outFile)
{
try
{
//load the Xml doc
XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
XslCompiledTransform myXslTrans = new XslCompiledTransform();
//load the Xsl
myXslTrans.Load(xslFileReader);
//create the output stream
XmlTextWriter myWriter = new XmlTextWriter
(outFile, null);
//do the actual transform of Xml
myXslTrans.Transform(myXPathDoc, null, myWriter);
myWriter.Close();
return "Done";
}
catch (Exception e)
{
return e.Message;
}
}