English 中文(简体)
地址:
原标题:Sorting XML with a namespace
  • 时间:2012-04-25 20:50:16
  •  标签:
  • .net
  • xml

我在分类XML查询时发现的大多数例子并不包括一个名称空间。 其中有些人这样做,但我找不到在有名字空间时进行询问和分类的任何例子。

 <Search xmlns="http://whatever.whatever.com/gg/whatever">
 <BinSet Type="Category">
 <Bin>
  <BinName>Clothing</BinName>
  <BinItemCount>804</BinItemCount>
   <BinParameter>
    <Name>Category</Name>
    <Value>Women s Apparel</Value>
  </BinParameter>
</Bin>
 <Bin>
  <BinName>Tools</BinName>
  <BinItemCount>126</BinItemCount>
   <BinParameter>
    <Name>Category</Name>
    <Value>Tools, handtools and hardware</Value>
  </BinParameter>
</Bin>
</BinSet>
</Search>

I don t have any control of the XML, so I have to deal with the namespace. Also, There are approximately 50 of these records in the XML and it is only part of a much larger XML file.

Dim nsuri as string = "http://whatever.whatever.com/gg/whatever"
Dim xpath as string = "dd:Search/dd:BinSet[Type="Category"]/*"
Dim nav As XPathNavigator = doc.CreateNavigator()
Dim exp As XPathExpression = nav.Compile(xpath)
Dim nsmgr As New XmlNamespaceManager(nav.NameTable)
nsmgr.AddNamespace("dd", nsuri)
exp.SetContext(nsmgr)

exp.AddSort("BinParameter/Value", XmlSortOrder.Ascending, XmlCaseOrder.UpperFirst, "en-US", XmlDataType.Text)

  now process in sorted order
Dim iterator As XPathNodeIterator = nav.Select(exp)
While iterator.MoveNext
    Dim nav2 As XPathNavigator = iterator.Current.Clone
    nav2.MoveToFirstChild()
    Debug.WriteLine(nav2.Value) 
End While

我所看到的所有例子都表明,如何把眼下的儿童节点划为一,例如我的XML。 但是,我需要所有这些信息,我需要把本巴拉参数/瓦莱诺德区分开来。 我尽一切可能想......

最后一点值得注意,在说并做所有事情时,我没有真正谨慎地利用XPathNodeIterator或XPathNavigator。 此前,我正在归还一位名叫XmlNodeList的XmlNodeList的所有记录,而我经常呼吁不要把这个名字编成联系,如:

<a href="mypage.aspx?si=Tools">Tools, handtools and hardware (126)</a>
<a href="mypage.aspx?si=Clothing">Women s Apparel (804)</a>

是否有任何人知道如何用名称空间将这种XML打上“Value”的被nes子?

此外,如果你知道以XmlNodeList或其他人来回报这种结果的最快方式。 净物体,以便我能够接触到所有价值:BinName, BinItemCount, 姓名和价值? I m 不熟悉XPathNavigator或XPathNodeIterator。

谢谢。

最佳回答

订立<代码>xpath 变数

/dd:Sets/dd:Bin/dd:BinParameter

and use the following AddSort line

exp.AddSort("dd:Value", XmlSortOrder.Ascending, XmlCaseOrder.UpperFirst, "en-US", XmlDataType.Text)

(note the dd: prefix to all elements)

EDIT:

Dim iterator As XPathNodeIterator = nav.Select(exp, nsmgr)
问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签