Getting data from xml by attribute value C# using XmlTextReader - xmltextreader

I have a file with the following content (myfile.xml). I have to get all content coming under (including product node) a product with id=1.
<products>
<product id="1">
<category>q</category>
</product>
<product id="2">
<category>w</category>
</product>
<product id="3">
<category>e</category>
</product>
</products>`
i.e. the result should be :
<product id="1">
<category>q</category>
</product>
How can I do this?
I have the restriction that I should use XmlTextReader or XPathNavigator only for this, otherwise I could have used this : Getting data from xml by attribute vlaue

Wouldn't XmlReader.GetAttribute() work?
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.getattribute(v=vs.100)
Add in some Skip() to move the iterator:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.skip(v=vs.100)

Related

Get XML attribute value of property using XPath

I have a XML like the example below:
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="0" failures="0" name="test" tests="1" time="2.747">
<properties>
<property name="categories" value="ExampleCategory" />
<property name="timestamp" value="1519664414463" />
</properties>
<testcase classname="com.example.junit.Test" name="test" time="2.747" />
</testsuite>
Is there a way to retrieve the property tag value according to the name of the property?
Right now, I'm using something like that:
#doc.xpath('//testsuite//properties//property/#value').text
This will give me "ExampleCategory1519664414463".
I know if I use .first or [0], [1], etc, I can get the values separately, but I couldn't find a way to get the values separately according to the "name" attribute.
Anyone know how can I retrieve that?
This XPath,
//property[#name='timestamp']/#value
will select all value attributes of property elements with a name attribute value equal to 'timestamp'.

Getting attribute with condition using XPath

I am using iReport 5.6.0 to generate a report with xml datasource .
I need to select the attribute date of a node that have an attribute named Type with a value of START
Since I'm new to iReport and XPath , i can't find the right XPath query .
I've tried this , but it didn't work :
<!-- language: lang-xml -->
<queryString language="xPath">
<![CDATA[/Document]]>
</queryString>
<field name="Date" class="java.lang.String">
<fieldDescription>
<![CDATA[Date/#Date[#type="START"]]]>
</fieldDescription>
</field>
Here 's my XML data file :
<!-- language: lang-xml -->
<?xml version='1.0' encoding="UTF-8"?>
<Document>
<Date Type="INV" Date="20140601" />
<Date Type="START" Date="20140201" />
</Document>
(I need the value : 20140201 to be displayed)
This is the XPath expression you're looking for:
Date[#Type="START"]/#Date
In natural language: get the Date attribute from Date elements that have a Type attribute with value "START".

Rabl: Multiple nodes with the same name

I have the following code for generating a xml from a rabl template:
obj = OpenStruct.new
obj.categories = [{node: ["Foo","Bar"]},{node: ["Test1","Test2"]}]
Rabl::Renderer.xml(obj, 'adapter_xml')
and this is the rabl template adapter_xml.rabl
object #obj => :root
attributes :categories
which generates this XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<categories>
<category>
<node>
<node>Foo</node>
<node>Bar</node>
</node>
</category>
<category>
<node>
<node>Test1</node>
<node>Test2</node>
</node>
</category>
</categories>
</root>
But what I want to achieve is the following format, without the extra <node> tags:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<categories>
<category>
<node>Foo</node>
<node>Bar</node>
</category>
<category>
<node>Test1</node>
<node>Test2</node>
</category>
</categories>
</root>
Is there any way to do this with rabl? Or do I have to modify the ruby code mentioned first?

How do I search a node with namespaces defined in the same node

If ancestor nodes defines namespaces, I can use them:
> Nokogiri::XML(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:creator opf:role="aut">John Doe</dc:creator>
</metadata>
</package>
XML
> xml.at_xpath("//dc:creator[#opf:role='aut']", xml.at_xpath("//xmlns:metadata").namespaces).text
=> "John Doe"
However, what shall I do with following XML?
> Nokogiri::XML(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
<metadata>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf" opf:role="aut">John Doe</dc:creator>
</metadata>
</package>
XML
> xml.at_xpath("//dc:creator[#opf:role='aut']", xml.at_xpath("//xmlns:metadata").namespaces).text
Nokogiri::XML::XPath::SyntaxError: Undefined namespace prefix: //dc:creator[#opf:role='aut']
I think xml.remove_namespaces! or literal namespace arguments for at_xpath is last resort.
To programmatically collect all the namespaces, use Document#collect_namespaces.
xml = Nokogiri::XML(xmldata)
ns = xml.collect_namespaces
puts xml.at('//dc:creator[#opf:role="aut"]', ns).text
Output:
John Doe

Getting data from xml by attribute vlaue

I have a file with the following content (myfile.xml). I have to get all content coming under (including product node) a product with id=1.
<products>
<product id="1">
<category>q</category>
</product>
<product id="2">
<category>w</category>
</product>
<product id="3">
<category>e</category>
</product>
</products>`
i.e. the result should be :
<product id="1">
<category>q</category>
</product>
How can I do this?
using XPath in Linq
var root = XElement.Load("myfile.xml");
root.XPathSelectElements( "/products/product[#id=1]");
var root = XElement.Load("path to the file");
var node = root.Descendants("product").FirstOrDefault(e=>e.Attribute("id").Value == "1");

Resources