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.
Related
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
I have a XML document which looks like :
<stReq xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EvalRes>
<RenDtSrc xmlns="http://fakeurl.com/somthing/facade/params">
<ContentType>application/pdf</ContentType>
<DocumentName>Name</DocumentName>
<Content>Doc Content</Content>
</RenDtSrc>
</EvalRes>
</stReq>
From a asp.net application, I am trying to check if a node <RenDtSrc> exists in a document or not. Following is the code I am using to read XML file and node element
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\Test\\Doc1.xml");
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/stReq/EvalRes/RenDtSrc");
Count of nodeList returns zero even though there are child nodes inside it.
I think its something to do with namespace manager but I can't figure it out. Any help would be appreciated.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\Test\\Doc1.xml");
var nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("s", "http://fakeurl.com/somthing/facade/params");
XmlNodeList nodeList = xmlDoc.SelectNodes("//s:RenDtSrc", nsm);
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("//stReq/EvalRes/node()/*");
Output:
Node [0] = {Element, Name="ContentType"}
Node [1] = {Element, Name="DocumentName"}
Node [2] = {Element, Name="Content"}
I want to fill my dropdownlist with all the payment methods, i should find the payment methods in an xml file that i have. this should be the xml code for the methods:
Dim xml As String
xml = "<?xml version=""1.0"" encoding=""UTF-8""?>"
xml &= "<gateways ua=""example-php-1.1"">"
xml &= "<merchant>"
xml &= " <account>123456</account>"
xml &= " <site_id>789</site_id>"
xml &= " <site_secure_code>112233</site_secure_code>"
xml &= "</merchant>"
xml &= "<customer>"
xml &= " <country>NL</country>"
xml &= " <locale>nl_NL</locale>"
xml &= "</customer>"
xml &= " </gateways>"
Dim apiURl As String
apiURl = "https://testapi.multisafepay.com/ewx/"
Dim httpWebRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(apiURl)
httpWebRequest.Method = "POST"
httpWebRequest.ContentLength = System.Text.Encoding.UTF8.GetByteCount(xml)
httpWebRequest.ContentType = "application/x-www-form-urlencoded"
Dim streamWriter = New System.IO.StreamWriter(httpWebRequest.GetRequestStream())
streamWriter.Write(xml)
streamWriter.Close()
Dim httpWebResponse As System.Net.HttpWebResponse = httpWebRequest.GetResponse()
Dim streamReader = New System.IO.StreamReader(httpWebResponse.GetResponseStream())
Dim stringResult = streamReader.ReadToEnd()
Dim xmlstring As String = stringResult
Dim xd As System.Xml.XmlDocument = New System.Xml.XmlDocument()
xd.LoadXml(xmlstring)
the stringResult gives this Value:
<?xml version="1.0" encoding="UTF-8"?>
<gateways result="ok">
<gateways>
<gateway>
<id>IDEAL</id>
<description>iDEAL</description>
</gateway>
<gateway>
<id> MASTERCARD</id>
<description>Visa via Multipay</description>
</gateway>
<gateway>
<id> BANKTRANS</id>
<description> Bank Transfer</description>
</gateway>
<gateway>
<id> VISA</id>
<description> Visa CreditCards</description>
</gateway>
</gateways>
</gateways>
i need to get the value between the <id></id> tags in my ddlMethod how can i do this?
You can use SelectNodes() method to get specific nodes from XmlDocument passing suitable XPath string as the method parameter. Since SelectNodes() returns collection of XmlNode, you also need to specify which property of XmlNode to be displayed in the dropdown list control. In this case, I assume you want to display the text between <id> tags so we use InnerText property :
Dim xd As System.Xml.XmlDocument = New System.Xml.XmlDocument()
xd.LoadXml(xmlstring)
'set data source of dropdown to all <id> elements from XML'
ddlMethod.DataSource = xd.SelectNodes("//id")
ddlMethod.DataTextField = "InnerText"
ddlMethod.DataValueField = "InnerText"
ddlMethod.DataBind()
Only if available in the .NET framework version you're using, I'd suggest to switch to XDocument which is more modern XML library in .NET compared to the older XmlDocument. In various situations XDocument is more friendly to use than XmlDocument. Example using XDocument and LINQ style :
Dim xd As XDocument = XDocument.Parse(xmlstring)
'set data soutce of dropdown to *content* of all <id> elements from XML'
ddlMethod.DataSource = xd.Descendants("id").Select(Function(x) x.Value)
ddlMethod.DataBind()
i came up with this and it works for me:
Dim bytTeller As Byte
For bytTeller = 0 To xd.GetElementsByTagName("gateway").Count - 1
Dim root As XmlNode = xd.GetElementsByTagName("gateway").Item(bytTeller)
ddlMethod.Items.Add(root.ChildNodes(0).InnerText)
Next
Okay I have a custom XML string that is provided so unable to modify structure it is a key value pair as seen here
<?xml version='1.0' encoding='utf-8'?>
<ContentStack ID='13' Type='11'>
<ContentElement1 ID='11' Type='IMAGE'>
<Key AttributeName='Name' AttributeValue='SOMENAME'></Key>
<Key AttributeName='SRC' AttributeValue='SOMEFILE.jpg'></Key>
<Key AttributeName='ALT' AttributeValue='SOMEALT'></Key>
<Key AttributeName='Class' AttributeValue='SOMECLASS'></Key>
</ContentElement1>
</ContentStack>
I want to be able to grab the attribute value and assign them to a variable so I end up with something like this.
Dim thsValueOne AS String = "SOMENAME"
Dim thsValueTwo AS String = "SOMEFILE.jpg"
Dim thsValueThree AS String = "SOMEALT"
Dim thsValueFour AS String = "SOMECLASS"
I have tried doing similar to this with no luck
Dim xDoc As XDocument = XDocument.Parse(" MY XML STING IN HERE ")
thsValueOne = xDoc.Descendants("Key.AttributeValue").Skip(0).Take(1).ToString
I'm thinking I'm no where near close.
Ok was able to get this figured out base on XML in question answer was simple
Dim xDoc As XDocument = XDocument.Parse(thsXmlContent)
thsValueOne = xDoc.Root.Descendants.<Key>(0).#AttributeValue
thsValueTwo = xDoc.Root.Descendants.<Key>(1).#AttributeValue
thsValueThree = xDoc.Root.Descendants.<Key>(2).#AttributeValue
thsValueFour = xDoc.Root.Descendants.<Key>(3).#AttributeValue
You can try out any of the below workarounds, in order to understand the concept.
As you are using VB.NET you can use XML literals:
Private Sub test1()
Dim xFrag = <?xml version='1.0' encoding='utf-8'?>
<ContentStack ID='13' Type='11'>
<ContentElement1 ID='11' Type='IMAGE'>
<Key AttributeName='Name' AttributeValue='SOMENAME'></Key>
<Key AttributeName='SRC' AttributeValue='SOMEFILE.jpg'></Key>
<Key AttributeName='ALT' AttributeValue='SOMEALT'></Key>
<Key AttributeName='Class' AttributeValue='SOMECLASS'></Key>
</ContentElement1>
</ContentStack>
Dim query = xFrag...<Key>, pos& = 0
For Each ele As XElement In query
pos += 1
MsgBox(String.Format("Test1:Key({0}):AttributeName='{1}' AttributeValue='{2}'", pos.ToString, ele.#AttributeName, ele.#AttributeValue))
Next ele
End Sub
Or otherwise use the Parse method.
Private Sub test2()
Dim xStr = "<?xml version='1.0' encoding='utf-8'?><ContentStack ID='13' Type='11'><ContentElement1 ID='11' Type='IMAGE'><Key AttributeName='Name' AttributeValue='SOMENAME'></Key><Key AttributeName='SRC' AttributeValue='SOMEFILE.jpg'></Key><Key AttributeName='ALT' AttributeValue='SOMEALT'></Key><Key AttributeName='Class' AttributeValue='SOMECLASS'></Key></ContentElement1></ContentStack>"
Dim xFrag = XDocument.Parse(xStr)
Dim query = xFrag...<Key>, pos& = 0
For Each ele As XElement In query
pos += 1
MsgBox(String.Format("Test2:Key({0}):AttributeName='{1}' AttributeValue='{2}'", pos.ToString, ele.#AttributeName, ele.#AttributeValue))
Next ele
End Sub
Hope this helps...
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