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;
Related
In the WCF service, I have implemented the IDispatchMessageInspector interface.
In the AfterReceiveRequest method ref Message request - the error "when reading the body: System.Xml.XmlException" comes. This error occurs due to an error in the XML in the request.I can't influence the request.
<data xsi:type="xsd:string"><?xml version="1.0" encoding="utf-8"?>
<someRequest>
<Number>Test</Number>
<Date>2023-01-09T00:00:00</Date>
</someRequest>
I'm trying to fix
request.toString().Replace("<?xml version="1.0" encoding="utf-8"?>" ,"");
Is it possible to get the xml body as text?
Tried:
using (var reader = request.GetReaderAtBodyContents())
{
query string var = reader.ReadContentAsString();
}
var body = request.getBody<string>();
If I implement the IDispatchOperationSelector interface. The method is called before AfterReceiveRequest. But there's also a Message parameter
To read an xml file, you can use XmlDocument.InnerXml to get it :
XmlDocument doc = new XmlDocument();
doc.Load("path to your file");
string xmlcontents = doc.InnerXml;
Or like this:
public string GetXMLAsString(XmlDocument myxml)
{
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
myxml.WriteTo(tx);
string str = sw.ToString();//
return str;
}
After getting it, then get rid of what you don't need.
I have following url which returns me XML response in c#:
http://www.bnr.ro/files/xml/years/nbrfxrates2017.xml
Now, i want to retrieve the currency in Euro for my date which is present as a paramater in my function. I want to use Linq but i have some problems
My function:
public static void getXml(String year, String data)
{
WebClient webClient = new WebClient();
string url = "http://www.bnr.ro/files/xml/years/nbrfxrates" + year + ".xml";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());
XElement xe = XElement.Load(response.GetResponseStream());
var x = (from e in xe.Elements("Cube")
where e.Attribute("date").Value.Equals(data)
select e.Element("Rate") into p
where p.Element("currency").Value.Equals(data)
select p.Value);
Console.WriteLine(x.ToString());
}
My error is:
The root element is missing on XElement xe =
XElement.Load(response.GetResponseStream());
Method GetResponseStream returns a stream:
Gets the stream that is used to read the body of the response from the
server.
You cannot read this stream twice. When you load XmlDocument it reads data from network stream and closes it releasing the connection. When you try to load XElement from the closed stream, you get an error.
You should read response stream only once - e.g. into string or into MemoryStream:
string xml;
using(var reader = new StreamReader(r.GetResponseStream()))
xml = reader.ReadToEnd();
Note: It's not clear why you need XmlDocument if you are using linq to xml.
I can't read xml string from http://158.58.185.214/Applications/Operator/Files/Data/Bus/CityList.xml and i think the encoding is the problem please help to solve it.
my code is:
string url = "http://158.58.185.214/Applications/Operator/Files/Data/Bus/CityList.xml";
WebClient client = new WebClient();
string xml = client.DownloadString(url);
but the xml string is:
‹ í½`I–%&/mÊ{JõJ×àt¡€`$Ø#ìÁˆÍæ’ìiG#....
your problem can be solve like this
using System.Xml;
String URLString = " http://localhost/books.xml";
XmlTextReader reader = new XmlTextReader (URLString);
while (reader.Read())
{
// Do some work here on the data.
Console.WriteLine(reader.Name);
}
Console.ReadLine();
refer this:https://support.microsoft.com/kb/307643/en-us
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.
I use the StreamReader class to obtain XML for my GeoCoding process from Google.
StreamReader srGeoCode = new StreamReader(WebRequest.Create(Url).GetResponse().GetResponseStream());
String GeoCodeXml = srGeoCode.ReadToEnd();
XmlDocument XmlDoc = new XmlDocument();
GeoCode oGeoCode = new GeoCode();
XmlDoc.Load(GeoCodeXml);
I get XML back but it adds \n and other extras to the XML
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<kml xmlns=\"http://earth.google.com/kml/2.0\"><Response>\n <name>
I have the same code in VB and it does not do this. I can successfully GeoCode my information using the VB version of this console app.
Is there a reason the C# version adds this extra data to the XML that I retrieve back? I am trying my best to convert everything over to C#. I enjoy coding in it over VB.
Here is the VB Code:
Dim wreqGeoCode As WebRequest = WebRequest.Create(strURL)
Dim wresGeoCode As WebResponse = wreqGeoCode.GetResponse
Dim srGeoCode As New StreamReader(wresGeoCode.GetResponseStream())
Dim strXML As String = srGeoCode.ReadToEnd()
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml(strXML)
You need XmlDoc.LoadXml if you're going to load a string. Load loads from a file.
BTW, the alternative is also more efficient. You can load the document directly from the stream:
WebRequest webRequest = WebRequest.Create(Url);
using (WebResponse webResponse = webRequest.GetResponse())
{
using (Stream responseStream = webResponse.GetResponseStream())
{
XmlDocument XmlDoc = new XmlDocument();
GeoCode oGeoCode = new GeoCode();
XmlDoc.Load(responseStream);
}
}
The using statements ensure that the WebResponse and Stream get cleaned up, even if an exception is thrown.
y not just do
GeoCodeXml=GeoCodeXml.Replace("\n","");
if it is truly returning the \n as mentioned here.