0

I'm trying to get an element and its siblings via XpathNavigator.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE document SYSTEM 'xmlschemas/domino_6_5_5.dtd'>
<document xmlns='http://www.lotus.com/dxl' version='6.5' maintenanceversion='5.0'>
    <item name='Keywords'><text/></item>
    <item name='Version'><number>1</number></item>
    <item name='UPDATEDISTRIBUTION'><text>1</text></item>
    <item name='$FILE' summary='true' sign='true' seal='true'>
        <object>
            <file hosttype='cdstorage' compression='none' flags='storedindoc' name='STG08828'>
                <created><datetime>20110119T230442,22+01</datetime></created>
            </file>
        </object>
    </item>
</document>

I want to navigate to the file-element, with the following XPath:

//item/name/object/file[@name='STG08828']

Why this path is wrong?

EDIT: Thanks for the hint with my "name"-mistake.

When I try to run it, I get nothing.

XmlElement rootNode = xmlDoc.DocumentElement;
// select the file Element
String query = "//file[@name='" + name + "']";
XmlNodeList fileElement = rootNode.SelectNodes(query);
float
  • 1,265
  • 5
  • 22
  • 38
  • 2
    `//item/name` is an attribute not an element. Try changing the xpath to `//item/object/file[@name='STG08828']` – Dustin Kingen Nov 11 '13 at 13:31
  • Are you sure they're the same case (the two `name` values) as XSLT is case sensitive? – freefaller Nov 11 '13 at 13:50
  • Yes, I'm using the xml file which i pasted above. There are only more item-elements in it. – float Nov 11 '13 at 14:10
  • I forgot to copy the DOCTYPE element... updated my post. – float Nov 11 '13 at 14:13
  • 1
    I know I'm clutching at strws, but are you sure that `name` (the variable that is used in `String query = "//file[@name='" + name + "']";`) is the same case and doesn't have any white-space in it? – freefaller Nov 11 '13 at 14:16
  • 2
    Your XML contains namespaces. It seems you did not register and use them. – Jens Erat Nov 11 '13 at 14:18
  • @freefaller yes I'm sure. The output is "//file[@name='STG08828']" – float Nov 11 '13 at 14:21
  • @JensErat How do I use them? I will search. – float Nov 11 '13 at 14:21
  • 2
    OK, @float, I'm just trying to remove obvious possibilities. I think Jens is on the right track with the namespacing. Look at the `XmlNamespaceManager` class – freefaller Nov 11 '13 at 14:24
  • Thank you, I appreciate that you try to help me :-) I will have a look. – float Nov 11 '13 at 14:27
  • Is there a way to ignore namespaces? I found how to Add the Namespace, but now the XML Reader which im forced to use requires the dtd. But actually I'm not validating something with the dtd or the namespace. – float Nov 11 '13 at 14:43
  • 2
    For example, have a look at [this answer](http://stackoverflow.com/questions/17315846/kissxml-xpath-and-default-namespace/17338847#17338847). As far as I know C# only supports XPath 1.0. – Jens Erat Nov 11 '13 at 15:09
  • Thank you. It is working. How could I add the missing dtd to the XmlDocument, so that I would user the namespace? – float Nov 11 '13 at 15:29

2 Answers2

1

I think you want:

//item/object/file[@name='STG08828']

Or maybe just:

//file[@name='STG08828']
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
0

I think you are missing your names space for xmlns='http://www.lotus.com/dxl'

Example:

XNamespace myMs = "http://www.lotus.com/dxl";
PhillyNJ
  • 3,859
  • 4
  • 38
  • 64