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))
Related
I have XML in SQL, I want the below result from the query.
TabSeq
Path
SubTabSeq
1
//Tab/Family
1
1
//Tab/Location
2
2
//Tab/Address
1
2
//Tab/Insurance
2
Below is the XML.
<Tabs>
<Tab sequence="001">
<Family sequence="001">
<Data>Yes</Data>
</Family>
<Location sequence="002">
<Data>USA</Data>
</Location>
</Tab>
<Tab sequence="002">
<Address sequence="001">
<Data>XYZ</Data>
</Address>
<Insurance sequence="002">
<Data>Yes</Data>
</Insurance>
</Tab>
</Tabs>
I have tried below,
declare #xml xml='<Tabs><Tab sequence="001"><Family sequence="001"><Data>Yes</Data></Family><Location sequence="002"><Data>USA</Data></Location></Tab><Tab sequence="002"><Address sequence="001"><Data>XYZ</Data></Address><Insurance sequence="002"><Data>Yes</Data></Insurance></Tab></Tabs>'
SELECT t.c.value('#sequence', 'nvarchar(100)') As TabSeq
FROM #xml.nodes('//Tabs/child::node()') as t(c)
SELECT '//Tab' + '/' + c.value('local-name(.)[1]','nvarchar(100)') AS Path, t.c.value('#sequence', 'nvarchar(100)') As SubTabSeq
FROM #xml.nodes('//Tab/child::node()') as t(c)
Is it possible?
In your second query you're iteraing subtabs, so you need to reference a parent of those nodes, which is Tab. Like with those two methods (depends on how strict you want to be):
SELECT
TabSeq1 = t.c.value('../#sequence', 'nvarchar(100)'),
TabSeq2 = t.c.value('./parent::Tab/#sequence', 'nvarchar(100)'),
'//Tab' + '/' + c.value('local-name(.)[1]','nvarchar(100)') AS Path,
t.c.value('#sequence', 'nvarchar(100)') As SubTabSeq
FROM #xml.nodes('//Tab/child::node()') as t(c)
Please try the following solution.
SQL
declare #xml XML =
N'<Tabs>
<Tab sequence="001">
<Family sequence="001">
<Data>Yes</Data>
</Family>
<Location sequence="002">
<Data>USA</Data>
</Location>
</Tab>
<Tab sequence="002">
<Address sequence="001">
<Data>XYZ</Data>
</Address>
<Insurance sequence="002">
<Data>Yes</Data>
</Insurance>
</Tab>
</Tabs>';
SELECT c.value('for $i in . return count(../../*[. << $i])', 'INT') AS [TabSeq]
, '//Tab/' + c.value('local-name(.)','nvarchar(100)') AS Path
, t.c.value('#sequence', 'INT') As SubTabSeq
FROM #xml.nodes('/Tabs/Tab/*') as t(c);
Output
TabSeq
Path
SubTabSeq
1
//Tab/Family
1
1
//Tab/Location
2
2
//Tab/Address
1
2
//Tab/Insurance
2
As mentioned, you are getting the wrong sequence attribute.
You can feed the result of one .nodes call into another using CROSS APPLY, so you can first shred the Tab nodes, then the child nodes
declare #xml xml='<Tabs><Tab sequence="001"><Family sequence="001"><Data>Yes</Data></Family><Location sequence="002"><Data>USA</Data></Location></Tab><Tab sequence="002"><Address sequence="001"><Data>XYZ</Data></Address><Insurance sequence="002"><Data>Yes</Data></Insurance></Tab></Tabs>'
SELECT
TabSeq1 = t2.child.value('#sequence', 'nvarchar(100)'),
TabSeq2 = t1.tab.value('#sequence', 'nvarchar(100)'),
Path = '/Tabs/Tab/' + t2.child.value('local-name(.)','nvarchar(100)'),
Data = t2.child.value('(Data/text())[1]', 'nvarchar(100)')
FROM #xml.nodes('/Tabs/Tab') t1(tab)
CROSS APPLY t1.tab.nodes('*') as t2(child)
db<>fiddle
Note that // descendant axis is slow, you should instead use the / child axis.
I try to add a XML file to SQL 2008.
My XML:
<ItemList>
<Section Index="0" Name="cat0">
<Item Index="0" Slot="0" />
<Item Index="1" Slot="0" />
</Section>
<Section Index="1" Name="cat1">
<Item Index="33" Slot="0" />
<Item Index="54" Slot="0" />
</Section>
<Section Index="2" Name="cat2">
<Item Index="55" Slot="0" />
<Item Index="78" Slot="0" />
</Section>
</ItemList>
SQL Column :
Name = Section Name,
Cat = Section Index,
Index = Item Index,
Slot = Item Slot.
My Example :
DECLARE #input XML = 'MY XML file'
SELECT
Name = XCol.value('#Index','varchar(25)'),
Cat = XCol.value('#Name','varchar(25)'),
[Index] = 'Unknown', /* Index from <Item>*/
Slot = 'Unknown' /* Slot from <Item> */
FROM #input.nodes('/ItemList/Section') AS test(XCol)
I don't know how to add values from "Item".
Thank you very much!
You can do it like this:
select
Name = XCol.value('../#Index','varchar(25)'),
Cat = XCol.value('../#Name','varchar(25)'),
[Index] = XCol.value('#Index','varchar(25)'),
Slot = XCol.value('#Slot','varchar(25)')
from
#input.nodes('/ItemList/Section/Item') AS test(XCol)
Key idea: take data one level deeper, not /ItemList/Section, but /ItemList/Section/Item. So in this case you are able to access attributes of Item and also you can access attributes of parent element (Section in your case) by specifying ../#Attribute_Name
Different than the previous answer - CROSS APPLY with the children Item nodes:
SELECT
Name = XCol.value('#Index','varchar(25)'),
Cat = XCol.value('#Name','varchar(25)'),
[Index] = XCol2.value('#Index','varchar(25)'),
Slot = XCol2.value('#Slot','varchar(25)')
FROM #input.nodes('/ItemList/Section') AS test(XCol)
CROSS APPLY XCol.nodes('Item') AS test2(XCol2)
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"))
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 reread chapter http://www.rebol.com/docs/core23/rebolcore-15.html on any and parse but can't achieve to parse this kind of hiearchical structure: is it possible ?
<Multipage>
<tab id=1>
<box id=1>
</box>
</tab>
<tab id=2>
<box id=2>
Hello
</box>
</tab>
<tab>
</tab>
<tab>
</tab>
</Multipage>
Gavin MacKenzie's xml-parse script at http://www.rebol.org/view-script.r?script=xml-parse.r1 will parse most XML data. As it is a generalised solution it is understandably more complex than a set of parse rules for a specific XML file.
Yes it's possible and not very hard :
data: {...}
ws-chars: charset " ^/^M^-"
ws: [any ws-chars]
rule: [
ws <Multipage> any [
ws "<tab" opt [ws "id=" copy id to ">" (print ["tab id:" id])] ">" any [
ws "<box" opt [ws "id=" copy value to ">" (print ["box id:" id])] ">"
opt [copy text to "<" (if text [?? text])]
</box>
]
ws </tab>
]
ws </Multipage> ws
]
parse/all data rule
Running this code, you'll get as output:
tab id: "1"
box id: "1"
text: "^/ "
tab id: "2"
box id: "2"
text: "^/ Hello^/ "