【问题标题】:How do I find a specific node that matches a variable value?如何找到与变量值匹配的特定节点?
【发布时间】:2019-08-14 00:18:03
【问题描述】:

有些人可能会说这个问题与上一个问题相似,但它也不同。 我有 ObjXmlSimpleTypeDoc 对象,它有两个 EnumerationValue 节点。每个 EnumerationValue 都有子节点。

我只想找到正确的 EnumerationValue 节点,它的 @code 与变量 strCourtNCIC 中的值匹配。在这个问题中 strCourtNCIC=MN010015J。 对于这个问题,strCourtNCIC 保存一个值 MN010015J。

如何在 VB.NET 中做到这一点?我的 VB.NET 代码为 objXmlEnumerationValueNode 返回 Nothing,即使我希望看到带有 @code = MN010015J 的节点

如何更改我的 VB.NET 代码行以找到具有与 strCourtNCIC 值匹配的@code 的 EnumerationValue 节点?

这里是对象 ObjXmlSimpleTypeDoc

<SimpleTypeCompanion enumerates="CourtLocationTextType">
    <EnumerationValue code="MN010015J">
        <Text>Emily County</Text>
        <AssociatedValue type="MNCISNodeID">
            <Text>111</Text>
        </AssociatedValue>
        <AssociatedValue type="CountyName">
            <Text>Emily</Text>
        </AssociatedValue>
        <AssociatedValue type="PhoneNumber">
            <Text>724-820-7123</Text>
        </AssociatedValue>
    </EnumerationValue>
    <EnumerationValue code="DC19DAKDC">
        <Text>Pope County</Text>
        <AssociatedValue type="MNCISNodeID">
            <Text>112</Text>
        </AssociatedValue>
        <AssociatedValue type="CountyName">
            <Text>Pope</Text>
        </AssociatedValue>
    </EnumerationValue>
</SimpleTypeCompanion>

这是我需要帮助才能获得与 strCourtNCIC (MN010015J) 匹配的正确 EnumerationValue 的 VB.NET 代码。

    'CourtNCIC 
strCourtNCIC = objXmlMNCISData.DocumentElement.SelectSingleNode("Case/Court/CourtNCIC").InnerText
'Access the CourtLocationTextType simple type. 
objXmlSimpleTypeDoc = Msc.Integration.CourtXml.Library.v4.SimpleType.GetCompanionFile("CourtLocationTextType")
'Get the correct EnumerationValue node that has @code =MN010015J string value
objXmlEnumerationValueNode = objXmlSimpleTypeDoc.SelectSingleNode("/SimpleTypeCompanion/EnumerationValue[@code=" + strCourtNCIC + "]/@code")

【问题讨论】:

  • 如果objXmlSimpleTypeDoc 是一个XElement,那么您可以使用objXmlEnumerationValueNode = objXmlSimpleTypeDoc.Elements("EnumerationValue").Where(Function(el) el.Attribute("code").Value = strCourtNCIC).ToList()。参考:Get XElement by value of its attribute.
  • objXmlSimpleTypeDoc 被声明为 Dim objXmlSimpleTypeDoc As System.Xml.XmlDocument。我收到一个错误 objXmlSimpleTypeDoc As XmlDocument 'Elements' is not a member of 'XmlDocument'
  • 是的,只有当 objXmlSimpleTypeDoc 是 XElement 时才有效,但我在问题中看不到它的声明,所以我只能猜测。
  • 您好 Andrew,感谢您对此的意见。根据您的知识,您建议我可以做什么?
  • 试试objXmlEnumerationValueNode = doc.SelectSingleNode("//SimpleTypeCompanion/EnumerationValue[@code='" &amp; strCourtNCIC &amp; "']"),参考:Search XML file for nodes with specific attribute value in .NET 2

标签: xml vb.net


【解决方案1】:

这是我的解决方案。这是值周围缺少单引号的情况!

objXmlEnumerationValueNode = objXmlSimpleTypeDoc.SelectSingleNode("/SimpleTypeCompanion/EnumerationValue[@code='" + strCourtORI + "']")

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 2018-10-25
    • 2016-02-17
    • 2014-07-21
    • 1970-01-01
    相关资源
    最近更新 更多