Read xml embedded in aspx page - asp.net

I want to read this source as xml
http://blabla/bla.aspxPageMethod=ElencoPresentiNew&idArea=0&dtRif=&mostra=T&format=xml
That returns a result like the following (straight in the browser, not as file)
I tried to use XmlDocument or Agilitypack tool, also httpwebrequest, but I get different problems. With xmldocument I get an error saying that some xml elements are missing, with the other methods I get the html source of the page but not the data.
How can I achieve what I want? Thanks
EDIT:
first xml lines from source:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Result>
<has_custom_url>false</has_custom_url>
<oggi>24.10.2019</oggi>
<utente>
<controlli196eseguiti>true</controlli196eseguiti>
<idprofiloutente>17</idprofiloutente>
<dizionario>
<u>U</u>
<e>E</e>
</dizionario>
<nominativo>super</nominativo>
<cambiopwd>1567402823277</cambiopwd>
<descrinsediamento>Insediamento principale</descrinsediamento>
EDIT 2:
Dim doc As XmlDocument = New XmlDocument()
doc.Load("http://192.168.2.49/checkandin/area.aspx?PageMethod=ElencoPresentiNew&idArea=0&dtRif=&mostra=T&format=xml")
Dim ns As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
Dim nodes As XmlNodeList = doc.SelectNodes("PresentiPerAreaCalc", ns)
Dim listaIniziale As New List(Of String)
For Each node As XmlNode In nodes
Dim record As String = node.SelectSingleNode("descrazienda").Value
listaIniziale.Add(record)
Next

Related

Converting string to XML node in VB.NET

I've an XML string in database column like this
<trueFalseQuestion id="585" status="correct" maxPoints="10"
maxAttempts="1"
awardedPoints="10"
usedAttempts="1"
xmlns="http://www.ispringsolutions.com/ispring/quizbuilder/quizresults">
<direction>You have NO control over how you treat customers.</direction>
<answers correctAnswerIndex="1" userAnswerIndex="1">
<answer>True</answer>
<answer>False</answer>
</answers>
</trueFalseQuestion>
But I need to do XML operations on this string like select its name, attributes values,inner text etc. How can I make this possible from this string
EDIT
Im sharing the code snippet I tried, but not working
Dim myXML As String
Dim gDt As New DataTable
gDt.Columns.Add("id")
gDt.Columns.Add("questionid")
gDt.Columns.Add("serial")
gDt.Columns.Add("direction")
Dim dr As DataRow
myXML ='<my above shared XML>'
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml(myXML)
dr = gDt.NewRow
dr("serial") = 1
dr("id") = xmlDoc.Attributes("id").Value
dr("direction") = xmlDoc("direction").InnerText
gDt.Rows.Add(dr)
But thats not working at all as I wish
There are many ways to parse XML in .NET, such as using one of the serialization classes or the XmlReader class, but the two most popular options would be to parse it with either XElement or XmlDocument. For instance:
Dim input As String = "<trueFalseQuestion id=""585"" status=""correct"" maxPoints=""10"" maxAttempts=""1"" awardedPoints=""10"" usedAttempts=""1"" xmlns=""http://www.ispringsolutions.com/ispring/quizbuilder/quizresults""><direction>You have NO control over how you treat customers.</direction><answers correctAnswerIndex=""1"" userAnswerIndex=""1""><answer>True</answer><answer>False</answer></answers></trueFalseQuestion>"
Dim element As XElement = XElement.Parse(input)
Dim id As String = element.#id
Or:
Dim input As String = "<trueFalseQuestion id=""585"" status=""correct"" maxPoints=""10"" maxAttempts=""1"" awardedPoints=""10"" usedAttempts=""1"" xmlns=""http://www.ispringsolutions.com/ispring/quizbuilder/quizresults""><direction>You have NO control over how you treat customers.</direction><answers correctAnswerIndex=""1"" userAnswerIndex=""1""><answer>True</answer><answer>False</answer></answers></trueFalseQuestion>"
Dim doc As New XmlDocument()
doc.LoadXml(input)
Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("q", "http://www.ispringsolutions.com/ispring/quizbuilder/quizresults")
Dim id As String = doc.SelectSingleNode("/q:trueFalseQuestion/#id", nsmgr).InnerText
Based on your updated question, it looks like the trouble you were having is that you weren't properly specifying the namespace. If you use XElement, it's much more forgiving (i.e. loose), but when you use XPath to select nodes in an XmlDocument, you need to specify every namespace, even when it's the default namespace on the top-level element of the document.

Loading xml file using asp.net return no records

I have this XML content:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Option1>false</Option1>
<Option2>http://www.google.com</Option2>
<Option3>false</Option3>
<Option4>false</Option4>
</configuration>
I trying to use code bellow to read this xml but Dataset.tables.count always return 0.
Dim dtConfig As New DataSet
Dim sArqXml as String = "asd.xml"
Dim xmlRoot As New XmlRootAttribute
xmlRoot.ElementName = "configuration"
xmlRoot.IsNullable = True
Dim xmlSer As XmlSerializer = New XmlSerializer(dtConfig.GetType, xmlRoot)
Dim fs As FileStream = New FileStream(sArqXml, FileMode.Open)
dtConfig = CType(xmlSer.Deserialize(fs), DataSet)
fs.Close()
How can I read this XML? The solution can be in c#.
To load a xml file into a dataset.
Imports System.Xml
Dim ds As New DataSet
ds.ReadXml("path_to_your_xml_file")
and I think it should be ds.Table(0).Rows.Count in your case

Please check my XPath query

please tell me where is problem. i wrote this xpath query but its not retriveing me any node.
i want to select "HotelName" from it:-
my selector code:
Dim xmlPath As String = Server.MapPath("aa.xml")
Dim doc As XmlDocument = New XmlDocument()
doc.Load(xmlPath)
Dim nodeList As XmlNodeList = doc.DocumentElement.SelectNodes("//HotelInfo/HotelName")
For Each child As XmlNode In nodeList
Response.Write("Node Name: " + child.Name)
Response.Write("Node Value:" + child.FirstChild.Value)
Next
my xml is like this:
<?xml version="1.0" encoding="utf-8"?>
<OTA_HotelDescriptiveInfoRS xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 OTA_HotelDescriptiveInfoRS.xsd" TimeStamp="2009-12-29T06:41:55-05:00" Version="1.006" PrimaryLangID="it" EchoToken="1" Target="Test">
<Success />
<Warnings>
<Warning Code="999" Type="2"> Your request's version is earlier than our supported version. We tried to process your request in case the versions are compatible. We support version 1.006 for the OTA_HotelDescriptiveInfoRQ call.</Warning>
</Warnings>
<HotelDescriptiveContents HotelCode="112" HotelCodeContext="HCL" HotelName="Hostal Cruz Sol" HotelCityCode="335">
<HotelDescriptiveContent HotelCode="112" HotelCodeContext="HCL" HotelName="Hostal Cruz Sol" HotelCityCode="335" CurrencyCode="EUR">
<HotelInfo>
<HotelName>Hostal Cruz Sol</HotelName>
Try this:
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ota", "http://www.opentravel.org/OTA/2003/05")
Dim nodeList As XmlNodeList =
doc.DocumentElement.SelectNodes("//ota:HotelInfo/ota:HotelName", nsmgr)
That xmlns added a default namespace, and you need to deal with it in your XPath expression.

System.InvalidOperationException: This document already has a 'DocumentElement' node

I have a trouble on appending new node to xmldocument (created in the memory). I have select the root node with the XmlDocument.SelectSingleNode() method, it work sometimes and in the other time it will give me "System.InvalidOperationException: This document already has a 'DocumentElement' node." error. More information, this xml document is multi level xml document.
By the way, when i try it with unit test it work fine (always), when i implement it in ASP.NET 3.5, it become weird, work sometimes and fail sometimes. Any idea, why this can help? All advise and suggestion are welcome.
Thanks.
You can select the root node of the XmlDocument using the Property "DocumentElement". Or i think you can use the Property "FirstChild" (untested).
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
XmlElement rootNode = doc.DocumentElement;
This worked for me.
xmlOriginFile = New XmlDocument()
xmlTargetFile = New XmlDocument()
xmlOriginFile.Load(readFile) //readFile is a string that hold path to xml document
xmlTargetFile.Load(writeFile) //writeFile is a string that hold path to xml document
Dim fileNav As XPathNavigator = xmlOriginFile.CreateNavigator()
Dim fileItr As XPathNodeIterator = fileNav.Select("//data")
Dim addToDestNodes As List(Of XmlNode) = New List(Of XmlNode)
While (fileItr.MoveNext())
Dim addNode As XmlNode = CType(fileItr.Current, IHasXmlNode).GetNode()
addToDestNodes.Add(addNode)
End While //loop thru nodes
If addToDestNodes.Count > 0 Then
For Each addedNode As XmlNode In addToDestNodes
Dim addTargetNode As XmlNode = xmlTargetFile.ImportNode(addedNode, True)
xmlTargetFile.DocumentElement.AppendChild(addTargetNode)
Next
End If
xmlTargetFile.Save(xmlTarget) //xmlTarget is a string that hold path to xml document
XML has a root element and you have to add new element within this root element.
XmlElement eleParent = docDestn.CreateElement("EleParent");
XmlElement eleChild = docDestn.CreateElement("Item");
eleParent.AppendChild(eleChild);
XMLNode rootNode= xmlDoc.SelectSingleNode("RootEle");
rootNode.AppendChild(eleParent);
Plps. refer the link for detail: http://navinpandit.blogspot.in/2016/12/exception-this-document-already-has.html

Error when trying to load XML from a Sharepoint Web Services call into an ASP.NET Gridview

I'm getting the error:
Illegal characters in path.
when trying to load XML, using XPath, into a gridview's datasource in the PageLoad function in the code-behind of an ASP.NET page I'm building. Does anyone know what this error means?
The XML coming in from the Sharepoint Web Services call is:
<?xml version='1.0' encoding='ISO-8859-1'?>
<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="2">
<z:row ows_Attachments="0" ows_LinkTitle="testTitle" ows_IncidentID="0" .../>
<z:row ows_Attachments="0" ows_LinkTitle="test2" ows_IncidentID="1" ... />
</rs:data>
And my code behind is:
newNode = thisL.GetListItems(strID, viewName, query, viewFields, rowLimit, queryOptions, webID)
mNodeList = newNode.ChildNodes
Dim ds_me As New Data.DataSet
xdsIncidents.Data = "<?xml version='1.0' encoding='utf-8'?>" & newNode.OuterXml
xdsIncidents.XPath = "//z:row"
GridView1.AutoGenerateColumns = True
DataDiv.InnerText = xdsIncidents.Data
ds_me.ReadXml(xdsIncidents.Data)
Dim dv As New Data.DataView(ds_me.Tables(1))
GridView1.DataSource = dv
GridView1.DataBind()
The error message is coming up on the line "ds_me.ReadXML..."
This is because that overload of ReadXml is expecting a file path to read some xml from, not a string with xml in it.
You'll need to create an XmlReader or TextReader from your string if you want to load the DataSet in this way.

Resources