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">
  <prizeUnit>
    <string_ID>333AI</string_ID>
    <prizeName>cashGrandPrize01</prizeName>
    <count>1313</count>
  </prizeUnit>
  <prizeUnit>
    <string_ID>334BI</string_ID>
    <prizeName>cashGrandPrize02</prizeName>
    <count>2424</count>
  </prizeUnit>
  <prizeUnit>
    <string_ID>335CI</string_ID>
    <prizeName>cashGrandPrize03</prizeName>
    <count>0</count>
  </prizeUnit>
</root_prizes>

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"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="root_prizes">
    <xs:complexType>
      <xs:sequence>
        <!--https://stackoverflow.com/questions/25409242/creating-an-xml-document-with-more-than-1-child-element-with-xml-schema-->
        <xs:element name="prizeUnit" maxOccurs="unbounded">
          <xs:complexType>
              <xs:sequence>
                <xs:element name="string_ID" type="xs:string" />
                <xs:element name="prizeName" type="xs:string" />
                <xs:element name="count" type="xs:integer" />
              </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

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

0
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}']");