I'm new to Xml, trying to create a simple database that I can read and write to from a corresponding C# console application.

I have the Xml file loaded, then generate a C# List of the Xml elements 'string_ID' (what was hoped to be the identifier in each node - will show code ** NOTE : not actual Xml ID feature). If the user input matches a string_ID within the List (I'm assuming I can cut this part, but that's basically the core of my problem), then the program selects the corresponding Xml element and increments the count node(?).

I cannot get selecting a single node, based from user input to work. Well, I can't get single node selection based on "string_ID" to work at all - as in hard-coding my test values.

Some of my previous attempts found on the web :

1) XmlElement root = xDoc.DocumentElement; var result = root.GetElementsByTagName("prizeUnit") .Where(i => (string)i.Element("string_ID") == "333AI") .Seelect(i => (string)i.Element("Folder"));

  • My most recent; .Where, etc. do not even register.

2) XmlNode node = xDoc.DocumentElement.SelectSingleNode( '//prizeUnit/string_ID[text()="333AI"]');

  • Note the single vs. double quotations.

3) XmlNode node = xDoc.DocumentElement.SelectSingleNode( "root_prizes/prizeUnit/string_ID[" + input + "]");

4) XmlNode node = xDoc.DocumentElement.SelectSingleNode( "root_prizes/prizeUnit/[string_ID/text() = \"input\"]");

  • This was the one that preceded case #2. The \" \" seemed noteworthy to me.

My Xml file :

<root_prizes xmlns="http://tempuri.org/XMLSchema.xsd">

My schema for the above file (I just jumped into schemas so apologies if there are glaring problems within) :

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
  <xs:element name="root_prizes">
        <xs:element name="prizeUnit" maxOccurs="unbounded">
                <xs:element name="string_ID" type="xs:string" />
                <xs:element name="prizeName" type="xs:string" />
                <xs:element name="count" type="xs:integer" />

user input is a string that should correspond to a "string_ID", i.e. '335AI'.

For case #3, I encounter the error : Unhandled Exception: System.Xml.XPath.XPathException: Expression must evaluate to a node-set.

Thank you for any and all help.

1 Answers

Alexander On Best Solutions

With help of XPath docs I've managed to build a solution for you

//if you have non-empty xmlns tag you have to use namespace manager
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ps", "http://tempuri.org/XMLSchema.xsd");

string id = "334BI";
XmlNode node = doc.SelectSingleNode($"/ps:root_prizes/ps:prizeUnit[ps:string_ID='{id}']", nsmgr);
//another of possible solutions
//XmlNode node = doc.SelectSingleNode($"descendant::ps:prizeUnit[ps:string_ID='{id}']", nsmgr);

If you had an empty xmlns tag or no tag at all, the following solution would work as well

XmlNode node = doc.SelectSingleNode($"/root_prizes/prizeUnit[string_ID='{id}']");