I have a System.Web.UI.WebControls.Xml control (Xml1) in a webforms app that I have upgraded from .NET 2.0 to .NET 4.0
I am getting two warnings from the code-behind page that I'd like to do something about.
...
Dim ds As DataSet = app.GetObjects
Dim xmlDoc As New System.Xml.XmlDataDocument(ds)
Xml1.Document = xmlDoc
Xml1.TransformSource = "~/xslt/admin_objectslist.xslt"
...
From the second line I get the warning:
'System.Xml.XmlDataDocument' is obsolete: 'XmlDataDocument class will be removed in a future release.'.
And from the third line I get the warning:
'Public Property Document As System.Xml.XmlDocument' is obsolete: 'The recommended alternative is the XPathNavigator property. Create a System.Xml.XPath.XPathDocument and call CreateNavigator() to create an XPathNavigator.
What is the recommended .NET 4.0 replacement for this?
ds.I ran into this problem with 3.5 as well. Here is what I came up with:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(ds.GetXml());
xml1.XPathNavigator = xmlDoc.CreateNavigator();
xml1.TransformSource = #"~/XSLT/LogEntryTransform.xslt";
Hope it helps.
Use Linq2XML - it's way more powerful than any of the other XML tools.... allows you to query and create/read/update/delete (CRUD) the XML just like you would a dataset or other strongly typed data source.
Once you get started with Linq you'll never go back to the old ways... it absolutely rocks!
Related
I am upgrading my solution from .net core 2.2 to 3.0, and I want to log the request data/body in the "proper" way. I am reading a lot that using the PipeReader is preferred to reading directly from the stream.
Previous to .net core 3.0, we used streams and the EnableRewind() method. It looked something like this:
HttpRequest.EnableRewind();
HttpRequest.Body.Position = 0;
var sr = new StreamReader(HttpRequest.Body);
var myData = sr.ReadToEnd();
HttpRequest.Body.Position = 0;
return myData;
I would like to understand how to properly use the PipeReader. Eg. what code is necessary to read in the HttpRequest body into a string? I see there is a ReadAsync() and TryRead() methods, but I'm not sure how to properly use these. I also see there is an AsStream() method which I've been able to use on the stream as I previously had (but without rewind).
I'd love to see any examples on how to do this, because it seems as though working with the pipe requires a great deal of pointer references. Lastly, if I work with the PipeReader.AsStream(), do I need to worry about rewind?
Here's how I do it:
HttpRequest.EnableBuffering();
using var streamReader = new StreamReader(HttpRequest.Body);
string data = await streamReader.ReadToEndAsync();
EnableBuffering() replaces EnableRewind().
I have an IIS server running with this asp code:
<%
Dim form
set form = Server.CreateObject("Library.Form")
form.Load "file.html"
Dim sStr = form.formhtml
Response.Write sStr
%>
How can I find out what are others functions and properties belong to form object? like Load and formhtml
Unfortunately I don't have the source code, so I try to do watch through attach to process with VS2015, but I not getting the inside properties and functions.
Create a new VB.NET solution (Windows Forms or Console) and paste that code.
Dim form
set form = Server.CreateObject("Library.Form")
form.Load "file.html"
Dim sStr = form.formhtml
You should be able to debug it inside of Visual Studio.
Since CreateObject uses late binding, in order to obtain full IntelliSense support I suggest adding a reference to the DLL of Library.Form object. Adding a reference is called early binding, with that you can browse all properties without even executing the code.
I have seen several post detailing how to get around the ReadOnly barrier in adding connection strings to ConfigurationManager.ConnectionStrings. See an example of one such post below. Along of each of these examples comes a dire, yet vague warning that the technique employed is "dangerous". What is the danger?
Dim cssc As ConnectionStringSettingsCollection = WebConfigurationManager.ConnectionStrings
Dim t As Type = cssc.GetType().BaseType ' System.Configuration.ConfigurationElementCollection
Dim fi As FieldInfo = t.GetField("bReadOnly", BindingFlags.Instance Or BindingFlags.NonPublic)
fi.SetValue(cssc, False)
One reason this is 'dangerous' is that you are relying on a private field named bReadOnly. That field is not part of the public .NET API and may change without notice in a future version of .NET. If that happens, and if you upgrade to that version, your code will no longer work.
I need to emulate an Excel Web Query in .net Below is the sample code. I get an Error500 when I attempt to do this in .net, however in Excel it works fine. Any ideas on what I am doing wrong? When I change the URI to a normal website it works fine, and returns the html from the page, which i what i am after. I wonder if the problem lies from the fact that I am trying to return a datatable
Dim oHttpWebRequest As System.Net.HttpWebRequest
Dim oStream As System.IO.Stream
Dim sChunk As String
oHttpWebRequest = (System.Net.HttpWebRequest.Create("http://somesite/foo.jsp"))
Dim oHttpWebResponse As System.Net.WebResponse = oHttpWebRequest.GetResponse()
oStream = oHttpWebResponse.GetResponseStream
sChunk = New System.IO.StreamReader(oStream).ReadToEnd()
oStream.Close()
oHttpWebResponse.Close()
Here is the Query from Excel
WEB
1
http:/somesite/foo.jsp
Selection=DataTable
Formatting=None
PreFormattedTextToColumns=True
ConsecutiveDelimitersAsOne=True
SingleBlockTextImport=False
DisableDateRecognition=False
DisableRedirections=False
Edit
I am getting the error when I getReponse from the server
I found the problem I was having.
I used fiddler to figure out the headers that were being sent via excel and compared those with the headers .net was sending
http://www.fiddler2.com/Fiddler2/version.asp
I had to add the following lines of code to add these two headers in order for it to work
oHttpWebRequest.Headers.Add(HttpRequestHeader.Pragma, "no-cache")
oHttpWebRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us")
I want to read an specific xml node and its value for example
<customers>
<name>John</name>
<lastname>fetcher</lastname>
</customer>
and my code behind should be some thing like this (I don't know how it should be though):
Response.Write(xml.Node["name"].Value)
As I said it is just an example because I don't know how to do it.
The most basic answer:
Assuming "xml" is an XMLDocument, XMLNodeList, XMLNode, etc...
Response.Write(xml.SelectSingleNode("//name").innerText)
Which version of .NET are you using? If you're using .NET 3.5 and can use LINQ to XML, it's as simple as:
document.Descendant("name").Value
(except with some error handling!) If you're stuk with the DOM API, you might want:
document.SelectSingleNode("//name").InnerText
Note that this hasn't shown anything about how you'd read the XML in the first place - if you need help with that bit, please give more detail in the question.
If using earlier versions of the .Net framework, take a look at the XMLDocument class first as this is what you'd load the XML string into. Subclasses like XMLElement and XMLNode are also useful for doing some of this work.
haven't tried testing it but should point you in the right direction anyway
'Create the XML Document
Dim l_xmld As XmlDocument
'Create the XML Node
Dim l_node As XmlNode
l_xmld = New XmlDocument
'Load the Xml file
l_xmld.LoadXml("XML Filename as String")
'get the attributes
l_node = l_xmld.SelectSingleNode("/customers/name")
Response.Write(l_node.InnerText)