Iam doing XML reading using dom concepts, But i couldn't retrieve values correctly.
I want to RETRIEVE BELOW PART using for loop statement
<area id="508" Type="paragraph" visible="1" source_ap="True" invert="False" rotation="0" ignoretext="0">
<pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename>
<pagenumber>1</pagenumber>
<left>603</left>
<top>868</top>
<right>764</right>
<bottom>1132</bottom>
<polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon>
</area>
HERE IS THE FULL SAMPLE XML WHERE I WANT TO RETRIEVE USING FOR LOOP.
<?xml version="1.0" encoding="UTF-8"?>
<articles>
<template istemplate="true" twidth="0" theight="0" uwidth="0" uheight="0" ubottom="0"> </template>
<article name="11187_2014_06_14_000002_0004_NFHEZ_00004" id="4">
<articlename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ_00004]]></articlename>
<input_pages>
<page name="11187_2014_06_14_000002_0004_NFHEZ" number="3">
<pagepdfheight>1125</pagepdfheight>
<pagepdfwidth>786</pagepdfwidth>
<pagejpgheight>1125</pagejpgheight>
<pagejpgwidth>786</pagejpgwidth>
<pagejpgresolution>72</pagejpgresolution>
<name><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></name>
<Article_Area>0</Article_Area>
<Article_percentage>0</Article_percentage>
</page>
</input_pages>
<NoOfPage>1</NoOfPage>
<output_pages>
<page number="1" height="0" width="0"/>
</output_pages>
<read>
<area id="508" Type="paragraph" visible="1" source_ap="True" invert="False" rotation="0" ignoretext="0">
<pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename>
<pagenumber>1</pagenumber>
<left>603</left>
<top>868</top>
<right>764</right>
<bottom>1132</bottom>
<polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon>
</area>
<area id="507" Type="paragraph" visible="1" source_ap="True" invert="False" rotation="0" ignoretext="0">
<pagename><![CDATA[11187_2014_06_14_000002_0004_NFHEZ]]></pagename>
<pagenumber>1</pagenumber>
<left>462</left>
<top>868</top>
<right>601</right>
<bottom>1131</bottom>
<polygon ispolygon="0" noofpoints="0"><![CDATA[]]></polygon>
</area>
</read>
My code is:
Dim doc As New XmlDocument()
Dim sValue, sPgName As String
Dim sLeft, sTops, sRight, sBottom As String
Dim ObjZoneInfo As CZoneInfo
Try
doc.Load(sFile)
If doc.ChildNodes.Item(0).Name = "xml" Then
If doc.ChildNodes.Item(1).Name = "articles" Then
For i As Integer = 0 To doc.ChildNodes.Item(1).ChildNodes.Count - 1
If doc.ChildNodes.Item(2).ChildNodes.Item(i).Name = "article" Then
ObjZoneInfo = New CZoneInfo
For j As Integer = 0 To doc.ChildNodes.Item(2).ChildNodes.Item(i).ChildNodes.Count - 1
sValue = doc.ChildNodes.Item(1).ChildNodes.Item(i).ChildNodes.Item(j).InnerText
If doc.ChildNodes.Item(1).ChildNodes.Item(i).ChildNodes.Item(j).Name = "page_name" Then
If sProFiles.Contains(sValue) = False Then
sProFiles.Add(sValue)
End If
ObjZoneInfo.PageName = sValue : sPgName = sValue
I understand you are searching only for nodes with "area" tag, so try this:
Dim tagAreas as XmlNodeList = doc.GetElementsByTagName("area")
You can then use a For Each loop:
For Each tagArea as XmlElement in tagAreas Then
'______your code here. If you wanted to retrieve the "area" tag I don't understand the code you posted
Next
Now, to access attributes of the area nodes, you must use the following:
Dim area_id as Integer = Integer.parse(tagArea.GetAttribute("id")) 'if you want to obtain id data as integer
Dim area_type as String = tagArea.GetAttribute("type")
'so etc___________________
if you want to access the child nodes:
Dim area_pagenum as Integer = Integer.parse(tagArea("pagenumber"))
Related
help use :
WebBrowser1.Document.GetElementById("xxxx").SetAttribute("value", "TESTE")
but I don't have the id, I need to use the name
codigo:
<input class="InputElement is-complete Input" autocomplete="xxxx" autocorrect="off" spellcheck="false" name="xxx" inputmode="numeric" aria-label="xxxx" placeholder="xxxx" aria-placeholder="xxxx" aria-invalid="false" value="xxxx">
Because more than one element can have the same name attribute, getElementsByName will return a node list of all elements with that name.
let x = document.getElementsByName("xxx");
This will set the "value" attribute for all elements with name "xxx".
for (let i = 0; i < x.length; i++) {
x[i].SetAttribute("value", "TESTE");
}
Edit: Sorry I thought you were using javascript.
In VB.net something like this should work
Dim allElements As HtmlElementCollection = WebBrowser1.Document.All
For Each Elem As HtmlElement In allElements
If Elem.GetAttribute("name") = "codigo-date" Then
Elem.SetAttribute("value", "TESTE)
edit: Elem.SetAttribute("value", "TESTE")
End If
Next
this is the html
<div id="catlist-listview" class="cat-listview cat-listbsize">
<ul>
<li>title1</li>
<li>title2</li>
<li>title3</li>
<li>title4</li>
<li>title5</li>
<li>title6</li>
<li>title7</li>
<li>title8</li>
<li>title9</li>
<li>title10</li>
</ul>
</div>
and my code is
dim htmldoc as new htmldocument
htmldoc.loadhtml(source)
for each link as htmlnode in htmldoc.document.selectnodes("//*[#id='catlist-listview']/ul")
textbox3.text = link.innerhtml
next
the output is
<li>title1</li>
<li>title2</li>
<li>title3</li>
<li>title4</li>
<li>title5</li>
<li>title6</li>
<li>title7</li>
<li>title8</li>
<li>title9</li>
<li>title10</li>
i want get all and only http://wantedlink1 to http://wantedlink10
i try attributes("href") but i get only one link
i want to list all the link like this :
http://wantedlink1
http://wantedlink2
http://wantedlink3
.
.
.
http://wantedlink10
any help ??
Basically, you can change XPath for SelectNodes() to be selecting individual <a> elements instead of <ul>. Then from this point, it will be easy to iterate through the result and get href attribute one by one. Or you achieve the same using LINQ, like the following for example :
'select <a> elements'
Dim links = htmldoc.Document.SelectNodes("//*[#id='catlist-listview']/ul/li/a")
'project to IEnumerable of href attribute value'
Dim hrefs = links.Cast(Of HtmlNode)().Select(Function(x) x.GetAttributeValue("href", ""))
'join the `hrefs`, separated by newline, into one string'
textbox3.text = String.Join(Environment.NewLine, hrefs)
dotnetfiddle demo
I've got customUIXml object of type XDocument and it has the following XML as value:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<ribbon>
<tabs>
<tab id="t1" label="Shalala">
<!-- stuff -->
</tab>
<tab id="tab_dev" label="SomeOtherTab">
<!-- stuff -->
</tab>
<tab id="t108" label="MyTab">
<!-- stuff -->
</tab>
</tabs>
</ribbon>
</customUI>
and I want to get the tab node with label value "MyTab". This is the code I use:
Dim xtab As XElement = Nothing
Dim nodes = From nodeToTake In customUIXml.Descendants().Elements("tab") _
Where nodeToTake.Attribute("label").Value = "MyTab"
Select nodeToTake
For Each tab As XElement In nodes
xtab = tab
Next
But I get no results and I can't seem to find what am I doing wrong...
You could simply use XML literals, so your code would be as easy as:
Dim xtab = customUIXml.<ribbon>.<tabs>.<tab>.First(Function(tab) tab.#label = "MyTab")
Also, your code seems to work, so your problem seems to be elsewhere.
Dim customUIXml = <customUI>
<ribbon>
<tabs>
<tab id="t1" label="Shalala">
<!-- stuff -->
</tab>
<tab id="tab_dev" label="SomeOtherTab">
<!-- stuff -->
</tab>
<tab id="t108" label="MyTab">
<!-- stuff -->
</tab>
</tabs>
</ribbon>
</customUI>
Dim xtab As XElement = Nothing
Dim nodes = From nodeToTake In customUIXml.Descendants().Elements("tab") _
Where nodeToTake.Attribute("label").Value = "MyTab"
Select nodeToTake
For Each tab As XElement In nodes
xtab = tab
Next
Console.WriteLine(xtab)
displays
<tab id="t108" label="MyTab">
<!-- stuff -->
</tab>
just fine.
If your actual XML contains a namespace, you have to take that into account:
...
Dim df As XNamespace = customUIXml.Name.Namespace
Dim xtab As XElement = Nothing
Dim nodes = From nodeToTake In customUIXml.Descendants().Elements(df + "tab") _
...
string xml = "<customUI xmlns='http://schemas.microsoft.com/office/2009/07/customui' onLoad='Ribbon_Load'><ribbon><tabs><tab id='t1' label='Shalala'><!-- stuff --></tab><tab id='tab_dev' label='SomeOtherTab'><!-- stuff --></tab><tab id='t108' label='MyTab'><!-- stuff --></tab></tabs></ribbon></customUI>";
var xelement = XElement.Parse(xml);
var list = xelement.Descendants().Where(x => x.Name.LocalName == "tab" && x.Attribute("label") != null).ToList();
list.ForEach(x => Console.WriteLine(x.Attribute("label").Value));
you can access localName to check the elment tag value ,
Checked with LinqPad and it works expectedly hope that helps..
:)
Edit : Vb code from Telerik convert :
Dim xml As String = "<customUI xmlns='http://schemas.microsoft.com/office/2009/07/customui' onLoad='Ribbon_Load'><ribbon><tabs><tab id='t1' label='Shalala'><!-- stuff --></tab><tab id='tab_dev' label='SomeOtherTab'><!-- stuff --></tab><tab id='t108' label='MyTab'><!-- stuff --></tab></tabs></ribbon></customUI>"
Dim xelement__1 = XElement.Parse(xml)
Dim list = xelement__1.Descendants().Where(Function(x) x.Name.LocalName = "tab" AndAlso x.Attribute("label") IsNot Nothing).ToList()
list.ForEach(Function(x) Console.WriteLine(x.Attribute("label").Value))
Ive read a few articles on Linq to XML and either ive picked it up wrong or missing some piece of the puzzle.
What im trying to achieve is to load some XML, get required data by different named fields and nodes/elements. Here is the XML
<?xml version="1.0" encoding="utf-8"?>
<metadata created="2014-05-15T12:26:07.701Z" xmlns="http://site/cu-2.0#" xmlns:ext="http://site/cu/b-2.0">
<customer-list count="47" offset="0">
<customer id="7123456" type="Cust" ext:mark="1">
<name>Tony Watt</name>
<sort-name>Watt, Tony</sort-name>
<gender>male</gender>
<country>US</country>
<knownAs-list>
<knownAs locale="ko" sort-name="Tony Watt"</knownAs>
<knownAs locale="ja" sort-name="Watt Tony"</knownAs>
</knownAs-list>
</customer>
<tag-list>
<tag count="1">
<name>Country</name>
</tag>
<tag count="1">
<name>usa</name>
</tag>
<customer id="9876543" type="Cust" ext:mark="2">
So i can load the XML and i can display data. Heres a snippet of the code
Dim ns As XNamespace = "http://site/cu-2.0#"
Dim XDoc As XDocument = XDocument.Parse(SomeXML)
For Each c As XElement In XDoc.Descendants(ns + "name")
Response.Write(c)
Next
So this displays all the elements with "name". The problem i have here is i want the customer name but not the tag-list country name (see last few lines of the XML)
Ideally i want to return all the details for each customer but adding the namespace limits the me to all the elements with name when i want other data too. If i remove the namespace i get no results returned so im unsure what to do next?
Ive read a ton of articles but i cant seem to work out what needs to be done or if ive gone down the wrong path? Please remember i have tried other methods which i can post if anyone likes but after reading MSDN and other articles i think ive confused myself or missed out a step.
I think you simply want to use
Dim ns As XNamespace = "http://site/cu-2.0#"
Dim XDoc As XDocument = XDocument.Parse(SomeXML)
For Each c As XElement In XDoc.Descendants(ns + "customer")
Response.Write(c.Element(ns + "name").Value)
Next
If you are trying to get name from customer give this a try
Dim xe As XElement =
<customer-list count="47" offset="0">
<customer id="7123456" type="Cust" mark="1">
<name>Tony Watt</name>
<sort-name>Watt, Tony</sort-name>
<gender>male</gender>
<country>US</country>
<knownAs-list>
<knownAs locale="ko" sort-name="Tony Watt"></knownAs>
<knownAs locale="ja" sort-name="Watt Tony"></knownAs>
</knownAs-list>
</customer>
<customer id="1" type="Cust" mark="1">
<name>Fred Flintstone</name>
<sort-name>Flintstone, Fred</sort-name>
<gender>male</gender>
<country>US</country>
<knownAs-list>
<knownAs locale="ko" sort-name="Fred Flintstone"></knownAs>
<knownAs locale="ja" sort-name="Flintstone Fred"></knownAs>
</knownAs-list>
</customer>
<tag-list>
<tag count="1">
<name>Country</name>
</tag>
<tag count="1">
<name>usa</name>
</tag>
</tag-list>
</customer-list>
Dim ie As IEnumerable(Of XElement) = From c As XElement In xe.Elements
Where c.Name.LocalName = "customer"
Select c From n As XElement In c.Elements
Where n.Name.LocalName = "name" Select n
For Each r As XElement In ie
Debug.WriteLine(r.Value)
Next
I have a radgrid populated with data in double click I launch a rad window manager with texbox that need to fill with the data selected on the radgrid. I fail to get the value of the row. I can only get the index of the selected item.
this is my grid aspx:
<telerik:RadGrid ID="rgBuscar" runat="server" CellSpacing="0" Culture="es-ES"
GridLines="None" Height="469px" Skin="Hay" Width="944px">
<ClientSettings>
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
</ClientSettings>
<MasterTableView>
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
<PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="True" />
<ClientEvents OnRowDblClick="RowDblClick"/>
</ClientSettings>
<PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
<FilterMenu EnableImageSprites="False"></FilterMenu>
and my JS:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function RowDblClick(sender, args) {
var index = args.get_itemIndexHierarchical();
sender.get_masterTableView().fireCommand("RowDblClick", index);
}
</script>
and finally my VB:
Protected Sub rgBuscar_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgBuscar.ItemCommand
If e.CommandName = "RowDblClick" Then
Dim var As String = e.CommandArgument
idValor = e.Item.Cells(2).Text
MostrarVentana(idValor)
End If
End Sub
Public Sub MostrarVentana(ByVal IdCampo As Integer)
lector = objBd.obtenerConfiguraciones("Cambio Ordenes")
While lector.Read
rwmCambio.Windows(0).NavigateUrl = lector("OCON_Url") & "?IdCampo=" & IdCampo
rwmCambio.Windows(0).Width = Unit.Pixel(lector("OCON_Width"))
rwmCambio.Windows(0).Height = Unit.Pixel(lector("OCON_Height"))
End While
rwmCambio.Windows(0).VisibleOnPageLoad = True
End Sub
Telerik provides a rich API for the RadGrid, by assigning a value to the DataKeyValues (or ClientDataKeyNamesif you want to access them using the Client API rather than posting back) attributes of the MasterTableView you can access the data related to the item through the code; to add multiple columns to the data key collection, separate the column names with a comma.
Example Data Key Definitions:
<MasterTableView DataKeyNames="idColumnName,foreignKeyColumnName" ClientDataKeyNames="idColumnName,anotherColumnName">
Example OnRowSelected event client-side (JavaScript) event handler:
function OnGridRowSelected(sender, args) {
var idDataKey = args.getDataKeyValue("idColumnName");
var nameDataKey = args.getDataKeyValue("idColumnName");
document.getElementById("myElement").value = "(" + idDataKey + ") " + nameDataKey;
}
Example ItemCommand event server-side (VB) event Handler:
Protected Sub PerformActionOnGridItem(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles grdCustomerAccountInvoiceSummary.ItemCommand
If (TypeOf (e.Item) Is Telerik.Web.UI.GridDataItem) Then
Dim item As Telerik.Web.UI.GridDataItem = CType(e.Item, Telerik.Web.UI.GridDataItem)
Select Case e.CommandName
Case "RowDblClick"
' Insert required code here
...
' Example: Set session variable to data key value
Session("idColumnName") = item.GetDataKeyValue("idColumnName")
End Select
End If
End Sub