XPath到我的XML中的元素 [英] XPath to elements in my XML
问题描述
我试图从xml文档中选择一些元素。 XPath的语法很难,我还没有找到一些好的教程,但是我有一个程序BaseX,它显示了一个到所选元素的XQuery路由。在C#代码中,我试图访问此元素并将其添加到列表中。但它抛出了这个错误:''/ descendant-or-self :: *:Test [.//*:Value contains textvalue1]''的标记无效。
任何人都可以从以下XML(不像/ DataSet)中的某些元素编写3个有效的XPath代码吗?有没有人知道XPath的一些好程序?或者任何人都可以解决错误并为我想要达到的元素写一个正确的XPath?
错误:
XmlNodeList list = xml.SelectNodes(tbXPath.Text);
C#代码:
protected void btnSelect_Click( object sender,EventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load( C:/ Users / Peta3nec / Documents / Visual Studio 2010 / WebSites / Beach_Volleyball /数据集/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 = 无界 >
< xs:element name < span class =code-keyword> = 论坛 >
< xs:complexType >
< xs:sequence >
< xs:element 名称 = ID t ype = xs:int / >
< xs:element name = 基准 类型 = xs:dateTime minOccurs = 0 / >
< xs:element name = Player_ID 类型 = xs:string minOccurs = 0 / >
< xs:element name = Zprava type = xs:string minOccurs = < span class =code-keyword> 0 / >
< / xs:sequence >
< / xs:complexType >
< / xs:element >
< xs:element 名称 = 测试 >
< xs:complexType >
< xs:sequence >
< xs:element 名称 = ID 类型 = xs:int / >
< xs:element 名称 = 值 类型 = xs:string minOccurs = 0 / >
< / xs:sequence >
< / xs:complexType >
< / xs :element >
< / xs:choice >
< / xs:complexType >
< xs:unique name = Constraint1 msdata:PrimaryKey = true >
< xs:selector xpath = .//论坛 / > ;
< xs:field xpath = ID / >
< / xs:unique >
< xs:unique 名称 = Test_Constraint1 msdata:ConstraintName = Constraint1 msdata:PrimaryKey = true >
< xs:selector xpath = .//测试 / >
< xs:field xpath = ID / >
< / xs:唯一 >
< / xs:element >
< / xs:schema >
< diffgr:diffgram xmlns:diffgr = urn:schemas-microsoft-com:xml-diffgram-v1 xmlns:msdata = urn:schemas-microsoft-com:xml-msdata >
< NewDataSet >
< 论坛 diffgr:id = Forum1 msdata:rowOrder = < span class =code-keyword> 0 diffgr:hasChanges = inserted >
< ID > 0 < / ID >
< 基准 > 2012-12-08T17 :30:11.677 + 01:00 < / Datum >
< Player_ID > Peta3nec < / P layer_ID >
< Zprava > Fórumjevprovozuděcka!< / Zprava >
< /论坛 >
< 论坛 diffgr:id = Forum2 msdata:rowOrder = 1 diffgr:hasChanges = inserted >
< ID > 1 < / ID >
< 基准 > 2012-12-08T17:30: 52.913 + 01:00 < / Datum >
< Player_ID > Dalo < / Player_ID >
< Zprava < span class =code-keyword>> 对于je GridView,jo? :-D < / Zprava >
< /论坛 >
< 测试 diffgr:id = Test1 msdata:rowOrder = 0 diffgr:hasChanges = inserted >
< ID > 1 < / ID > ;
< 值 > value0 < / Value > ;
< / Test >
< 测试 diffgr:id = Test2 msdata:rowOrder = 1 diffgr:hasChanges = inserted >
< ID > 2 < / ID >
< 值 > value1 < / Value >
< / Test >
< 测试 diffgr:id = Test3 msdata:rowOrder = 2 diffgr:hasChanges = inserted >
< ID > 3 < / ID >
< 值 > value2 < / Value >
< /测试 >
< 测试 diffgr:id = Test4 msdata:rowOrder = 3 diffgr:hasChanges = inserted < span class =code-keyword>>
< ID > 4 < / ID < span class =code-keyword>>
< 价值 > value3 < / Value >
< / Test >
< 测试 diffgr:id = Test5 msdata :rowOrder = 4 diffgr:hasChanges = inserted >
< < span class =code-leadattribute> ID > 5 < / ID >
< < span class =code-leadattribute>值 > value4 < /值 >
< < span class =code-leadattribute> / Test >
< 测试 diffgr:id = Test6 msdata:rowOrder = 5 diffgr:hasChanges = inserted >
< ID > 6 < / ID >
< 值 > value5 < / Value >
< / test >
< ; 测试 diffgr:id = Test7 msdata:rowOrder = 6 diffgr:hasChanges = inserted >
< ID > 7 < / ID >
< 值 > value6 < / Value >
< / Test >
< 测试 diffgr:id = Test8 msdata:rowOrder = 7 diffgr:hasChanges = inserted >
< ID > 8 < / ID >
< 价值 > value7 < / Value >
< / Test >
< 测试 diffgr:id = Test9 msdata:rowOrder = 8 diffgr:hasChanges = inserted >
< ID > 9 < / ID >
< 值 > value8 < / Value >
< / Test >
< 测试 diffgr:id = Test10 msdata:rowOrder = 9 diffgr:hasChanges = inserted >
< ID > 10 < / ID >
< 值 > value9 < / Value >
< / Test < span class =code-keyword>>
< / NewDataSet >
< / diffgr:diffgram >
< < span class =code-leadattribute> / DataSet >
使用xPath的文章和示例代码操纵XML XPath和XmlDocument(C#)的数据 [ ^ ]
和
http://www.w3schools.com/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>
Article and sample code for using xPath Manipulate XML data with XPath and XmlDocument (C#)[^]
and
http://www.w3schools.com/xpath/[^]
这篇关于XPath到我的XML中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!