Click here to Skip to main content
11,410,118 members (61,606 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML .NET XPath
Hi,
I am trying to select some elements from the xml document. Syntax of XPath is hard, I haven't found some good tutorial for it, but I have a program BaseX, which displays a XQuery route to the selected element. In a C# code I am trying to reach this element and add it to a list. but it throws this error: '/descendant-or-self::*:Test[.//*:Value contains text "value1"]' has an invalid token.
Can anyone write 3 valid XPath codes to some elements from the following XML (not like /DataSet)? Does anybody know about some good program for XPath? Or can anyone solve the error and write a correct XPath to the element I am trying to reach?

Error at:
XmlNodeList list = xml.SelectNodes(tbXPath.Text);

C# code:
protected void btnSelect_Click(object sender, EventArgs e)
{
        XmlDocument xml = new XmlDocument();
        xml.Load("C:/Users/Peta3nec/Documents/Visual Studio 2010/WebSites/Beach_Volleyball/DataSets/DataSet_Zkouška.xml");
        XmlNodeList list = xml.SelectNodes(tbXPath.Text);
        GridView1.DataSource = list;
        GridView1.DataBind();
}

XML:
<DataSet>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Forum">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int"/>
                <xs:element name="Datum" type="xs:dateTime" minOccurs="0"/>
                <xs:element name="Player_ID" type="xs:string" minOccurs="0"/>
                <xs:element name="Zprava" type="xs:string" minOccurs="0"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="Test">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int"/>
                <xs:element name="Value" type="xs:string" minOccurs="0"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Forum"/>
        <xs:field xpath="ID"/>
      </xs:unique>
      <xs:unique name="Test_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Test"/>
        <xs:field xpath="ID"/>
      </xs:unique>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <NewDataSet>
      <Forum diffgr:id="Forum1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
        <ID>0</ID>
        <Datum>2012-12-08T17:30:11.677+01:00</Datum>
        <Player_ID>Peta3nec</Player_ID>
        <Zprava>Fórum je v provozu děcka!</Zprava>
      </Forum>
      <Forum diffgr:id="Forum2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
        <ID>1</ID>
        <Datum>2012-12-08T17:30:52.913+01:00</Datum>
        <Player_ID>Dalo</Player_ID>
        <Zprava>To je GridView, jo? :-D</Zprava>
      </Forum>
      <Test diffgr:id="Test1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
        <ID>1</ID>
        <Value>value0</Value>
      </Test>
      <Test diffgr:id="Test2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
        <ID>2</ID>
        <Value>value1</Value>
      </Test>
      <Test diffgr:id="Test3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
        <ID>3</ID>
        <Value>value2</Value>
      </Test>
      <Test diffgr:id="Test4" msdata:rowOrder="3" diffgr:hasChanges="inserted">
        <ID>4</ID>
        <Value>value3</Value>
      </Test>
      <Test diffgr:id="Test5" msdata:rowOrder="4" diffgr:hasChanges="inserted">
        <ID>5</ID>
        <Value>value4</Value>
      </Test>
      <Test diffgr:id="Test6" msdata:rowOrder="5" diffgr:hasChanges="inserted">
        <ID>6</ID>
        <Value>value5</Value>
      </Test>
      <Test diffgr:id="Test7" msdata:rowOrder="6" diffgr:hasChanges="inserted">
        <ID>7</ID>
        <Value>value6</Value>
      </Test>
      <Test diffgr:id="Test8" msdata:rowOrder="7" diffgr:hasChanges="inserted">
        <ID>8</ID>
        <Value>value7</Value>
      </Test>
      <Test diffgr:id="Test9" msdata:rowOrder="8" diffgr:hasChanges="inserted">
        <ID>9</ID>
        <Value>value8</Value>
      </Test>
      <Test diffgr:id="Test10" msdata:rowOrder="9" diffgr:hasChanges="inserted">
        <ID>10</ID>
        <Value>value9</Value>
      </Test>
    </NewDataSet>
  </diffgr:diffgram>
</DataSet>
Posted 11-Dec-12 3:23am
Comments
CHill60 at 11-Dec-12 10:00am
   
What is in tbXPath.Text ?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 169
1 OriginalGriff 110
2 Maciej Los 68
3 BillWoodruff 60
4 Philippe Mori 60
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100