Get value through one of the xmlnode property for currentnode - vb.net

My current xml node is :
<Item xsi:type="itm:Resource">
<ID>10</ID>
</Item>
I want to read the whole tag and search whether Resource is there or not in that tag :
SelectSingleNode.OuterXml.Contains("Resource")
But Outer xml is considering all the tags inside Item , I just want for the current node
Have tried other properties like name,value which indeeds return only "Item"

I have done it in a slightly different way
SelectSingleNode("xPath").Attributes(0).Value.Contains("Resource")
Hope this helps

Related

SQL get value from XML in tag, by tag value

I have the following XML:
<Main>
<ResultOutput>
<Name>TEST1</Name>
<Value>D028</Value>
</ResultOutput>
<ResultOutput>
<Name>TEST2</Name>
<Value>Accept</Value>
</ResultOutput>
<ResultOutput>
<Name>TEST3</Name>
<Value />
</ResultOutput>
</Main>
What I want is to get the value of the <value> tag in SQL.
Basically want to say get <value> where <Name> has the value of TEST1, as an example
This is what I have at the moment, but this depends on the position of the XML tag:
XMLResponse.value(Main/ResultOutput/Value)[5]', nvarchar(max)')
The best way to do this is not to put extra where .value clauses, but to do it directly in XQuery.
Use [nodename] to filter by a child node, you can even nest such predicates. text() gets you the inner text of the node:
XMLResponse.value('(/Main/ResultOutput[Name[text()="TEST1"]]/Value/text())[1]', 'nvarchar(max)')
Below is an example using the sample XML in your question. You'll need to extend this to add namespace declarations and the proper xpath expressions that may be present in your actual XML as your query attempt suggests.
SELECT ResultOutput.value('Value[1]', 'nvarchar(100)')
FROM #xml.nodes('Main/ResultOutput') AS Main(ResultOutput)
WHERE ResultOutput.value('Name[1]', 'nvarchar(100)') = N'TEST1';

Export SQL XML field to grid [duplicate]

I have something like the following XML in a column of a table:
<?xml version="1.0" encoding="utf-8"?>
<container>
<param name="paramA" value="valueA" />
<param name="paramB" value="valueB" />
...
</container>
I am trying to get the valueB part out of the XML via TSQL
So far I am getting the right node, but now I can not figure out how to get the attribute.
select xmlCol.query('/container/param[#name="paramB"]') from LogTable
I figure I could just add /#value to the end, but then SQL tells me attributes have to be part of a node. I can find a lot of examples for selecting the child nodes attributes, but nothing on the sibling atributes (if that is the right term).
Any help would be appreciated.
Try using the .value function instead of .query:
SELECT
xmlCol.value('(/container/param[#name="paramB"]/#value)[1]', 'varchar(50)')
FROM
LogTable
The XPath expression could potentially return a list of nodes, therefore you need to add a [1] to that potential list to tell SQL Server to use the first of those entries (and yes - that list is 1-based - not 0-based). As second parameter, you need to specify what type the value should be converted to - just guessing here.
Marc
Depending on the the actual structure of your xml, it may be useful to put a view over it to make it easier to consume using 'regular' sql eg
CREATE VIEW vwLogTable
AS
SELECT
c.p.value('#name', 'varchar(10)') name,
c.p.value('#value', 'varchar(10)') value
FROM
LogTable
CROSS APPLY x.nodes('/container/param') c(p)
GO
-- now you can get all values for paramB as...
SELECT value FROM vwLogTable WHERE name = 'paramB'

XPath query on node-set working like a SQL where in

a sample of xml document:
<xml>
<list>
<item refid="1" />
<item refid="3" />
</list>
<catalogue>
<model id="1"><details /></model>
<model id="2"><details /></model>
<model id="3"><details /></model>
</catalogue>
</xml>
I'd like to query something like //model[ #id = (//item/#refid) ]
to obtain all "model" having a referenced id in "list"
I'd like to query something like
//model[ #id = (//item/#refid) ] to
obtain all "model" having a referenced
id in "list"
The main problem here is your lack of confidence and not actually running an XPath engine to evaluate the expressions you've come up with.
If you evaluate the XPath expression you proposed:
//model[ #id = (//item/#refid) ]
You'll see that it selects exactly the (two) model elements, whose id attributes are referenced by the refid attributes of item elements that are children of list.
#Jörn-Horstmann in his answer already explained why you get these results.
A minor remark is to generally avoid using the // abbreviation. It causes the whole document to be scanned and is very inefficient. In this case I would use the equivalent but probably faster to evaluate XPath expression:
/*/catalogue/model[#id = /*/list/item/#refid]
Your xpath expression should already return exactly what you want. Quoting from http://www.w3.org/TR/xpath/#booleans, 5th paragraph:
If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true

How do I loop nodes using NSXML on the Mac and change each node's text value

have been stuck on this for days now- How can I loop every node in an XML document and change the text value of the node.
For example go from this:
<root>
<node1>some text</node1>
<node2>
<node3>some more text</node3>
</node2>
</root>
to something like:
<root>
<node1>updated text</node1>
<node2>
<node3>updated text</node3>
</node2>
</root>
The code I have that doesn't work is:
NSArray *nodes = [xmlDoc nodesForXPath:#"//*"];
for (NSXMLElement *node in nodes) {
//In time a function call will go here to change the text:
NSString *newVal = #"updated text";
[node setStringValue:newVal];
}
Although it seems to loop ok when i check the contents of XMLDoc it then has this in it:
<root>
updated text
</root>
Please help if you can I have tried repeated google searches and am pulling my (already thinning) hair out - surely this should be fairly simple?!
Matt.
It's your xpath expression. //* selects all nodes in the document including the root node. So at some point in the iteration you are setting the string value of the root node which is apparently wiping out all its previous child nodes.
I'm not an expert in xpath, but something like:
/root//*
might do the trick except that node3 will get wiped out for the same reasons. If you look through The XPath tutorial there should be a way in there of selecting all text nodes.

Not able to get desired output while generating xml files from SQL query from SQL Server

I am executing this query
select category "ROOT/category",
question "Category/question",
option1 "Category/option1"
from testDB2 for XML PATH ('ROOT') , ELEMENTS
Presently the database has three entries and the xml file i get is this
<ROOT>
<ROOT>
<category>maths</category>
</ROOT>
<Category>
<question>2+2?</question>
<option1>1</option1>
</Category>
</ROOT>
<ROOT>
<ROOT>
<category>maths</category>
</ROOT>
<Category>
<question>100*0</question>
<option1>0</option1>
</Category>
</ROOT>
<ROOT>
<ROOT>
<category>chemistry</category>
</ROOT>
<Category>
<question>H2O?</question>
<option1>water </option1>
</Category>
</ROOT>
I do not want this, i want a file with just one main Parent node and rest of them as its child and each child can be parent for other child nodes, but there should be just one single main Parent node, in this case each row is a separate parent and there is no main or single parent
I hope I am able to tell my question properly. Thanks
try something like this:
select category,
question,
option1
from testdb2
for xml raw('Category'), elements, root('Categories')
for xml raw: this will make a node for each row in your table, with every column an attribute for that node
for xml raw('user'): this is the same as xml raw, but you specify the name of the nodes
for xml raw('user') elements: you swith from a attribute view to a node view. every column will be a node in your row node
root('Users'): you can use this to name your parent root
hope this helps
I think you want to use the FOR XML AUTO mode to shape your output.
Not 100% sure what it is you really want, but how about this:
SELECT
category '#Name',
question "Category/question",
option1 "Category/option1"
FROM
dbo.testDB2
FOR XML PATH('Category'), ROOT('ROOT')
Does that get closer to what you want? If I'm not mistaken (can't test right now), this should give you something like:
<ROOT>
<Category Name="maths">
<question>100*0</question>
<option1>0</option1>
</Category>
<Category Name="chemistry">
<question>H2O?</question>
<option1>water </option1>
</Category>
</ROOT>
If not - could you post a few sample rows of data, and what you expect to get from your SELECT in the end??
Marc