我有以下xml代码,我想找到具有相同属性值的节点总数。 代码:
<?xml version="1.0" encoding="UTF-8"?>
<gfx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Gfx-SE8.xsd">
<displaySettings displayType="replace" position="useCurrentPosition" securityCode="*" backColor="white" titleBar="true" titleBarText="" maximumTagUpdateRate="1" focusHighlightColor="lime" disableFocusHighlight="false" size="useCurrentSize" width="960" height="540" allowMultipleRunningCopies="false" cacheAfterDisplaying="false" systemMenu="true" minimizeButton="true" sizeToMainWindow="false" showLastAcquiredValue="true" TrackScreenForNavigation="true" TrackName="" allowResizing="false" whenResized="scale" beepOnPress="false" highlightWhenCursorPassesOver="true" interactiveHighlightColor="black" displayOnScreenKeyboard="false" allowButtonActionOnError="true" fieldNotSelectedTextColor="black" fieldNotSelectedFillColor="white" fieldSelectedTextColor="black" fieldSelectedFillColor="white" fieldInErrorNotSelectedTextColor="black" fieldInErrorNotSelectedFillColor="red" fieldInErrorSelectedTextColor="white" fieldInErrorSelectedFillColor="red" startupCommand="" shutdownCommand="" useGradientStyle="false" endColor="teal" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight"/>
<button name="Button1" height="112" width="293" left="160" top="80" visible="true" wallpaper="false" toolTipText="" exposeToVba="vbaControl" isReferenceObject="false" style="3d" captureCursor="false" highlightOnFocus="true" tabIndex="1">
<confirm confirmAction="false" buttonSetting="okCancel" titleBar="true" titleBarText="Confirmation" windowPosition="Centered of screen">
<caption fontFamily="Arial" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Are you sure you want to perform this action?"/>
<imageSettings imageReference="noImage"/>
</confirm>
</button>
<numericDisplay name="NumericDisplay1" height="20" width="264" left="524" top="111" visible="true" wallpaper="false" toolTipText="" exposeToVba="vbaControl" isReferenceObject="false" backColor="white" backStyle="transparent" foreColor="black" fontFamily="Arial" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" justification="right" fieldLength="22" showDigitGrouping="false" decimalPlaces="0" format="decimal" overflow="showExponent" leadingCharacter="blanks">
<connections>
<connection name="Value" expression="5"/>
</connections>
</numericDisplay>
<stringDisplay name="StringDisplay1" height="20" width="276" left="552" top="168" visible="true" wallpaper="false" toolTipText="" exposeToVba="vbaControl" isReferenceObject="false" backColor="white" backStyle="transparent" foreColor="black" fontFamily="Arial" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" justification="left" dimensionsHeight="1" dimensionsWidth="23" characterOffset="0">
<connections>
<connection name="Value" expression="s"/>
</connections>
</stringDisplay>
</gfx>
从上面的代码我想找到具有属性值exposeToVba="vbaControl"
的节点数。
我只想要节点总数。
我正在使用以下代码进行其他操作,我只想在上面添加一个代码。
`Dim xmldoc As New XmlDataDocument()
'Dim xmldoc As New XDocument
Dim iList As XmlNodeList
Dim iNode As XmlNode
Dim iAtt As XmlAttributeCollection
Dim nAtri As XmlAttribute
Dim fs As New FileStream(file, FileMode.Open, FileAccess.Read)
xmldoc.Load(fs)
'************Loop for FTView SE and ME***************************'
If RBSe.Checked = True Or RBMe.Checked = True Then
Dim attrName As String = "exposeToVba"
Dim attrValue As String = "vbaControl"
iList = xmldoc.GetElementsByTagName("displaySettings")
worksheet.Cells(row, col).value = xmldoc.LastChild.ChildNodes.Count - 2 '***For total objects count***
Dim Globalobjectscount = xmldoc.GetElementsByTagName("parameters") '***For Global object count***
worksheet.Cells(row, col + 1).value = Globalobjectscount.Count
worksheet.Cells(row, col + 2).value = xmldoc.GetElementsByTagName("animations").Count
worksheet.Cells(row, col + 3).value = xmldoc.GetElementsByTagName("numericDisplay").Count
worksheet.Cells(row, col + 4).value = xmldoc.GetElementsByTagName("numericInput").Count
worksheet.Cells(row, col + 5).value = xmldoc.GetElementsByTagName("Button").Count
For Each iNode In iList
iAtt = iNode.Attributes
`
答案 0 :(得分:3)
使用XDocument类的Linq To Xml解决方案:
Dim xcon As String = "the content of your xml file"
'use XDocument.Load(xmlFileName) to load Xml fIle
Dim xdoc As XDocument = XDocument.Parse(xcon)
Dim attrName As String = "exposeToVba"
Dim attrValue As String = "vbaControl"
Dim myCount As Integer = xdoc.Descendants() _
.Where(Function(x) x.Attribute(attrName) = attrValue).Count()
'returns 3
尝试!
有关详细信息,请参阅:
Linq To Xml
XDocument class