【问题标题】:Read XML Value By Attribute Name按属性名称读取 XML 值
【发布时间】:2018-12-05 12:46:35
【问题描述】:
<Customer>
    <Type H="General Information" ID="GeneralInfo">
         <Row>
             <C H="Customer Name">Mr. Robert</C>
             <C H="Relation">S/O. John</C>
             <C H="Date of Birth">01/01/1985</C>
        </Row>
    </Type>
    <Type H = "Other Details" ID = "ShareDet">
         <Row>
            <C H = "Address 1">XYZ</C>
            <C H = "Address 2">ABC</C>
        </Row>
    </Type>
</Customer>

我试图在 C# 中从上面的 XML 中读取“罗伯特先生”,但我做不到。 我试过下面的代码:

XmlDocument objXmlMain = new XmlDocument();
objXmlMain.LoadXml("Loading_Above_XMLSTRING");
string test = objXmlMain.SelectSingleNode("Customer/Type/Row/C/@H").Value;

我得到的结果是“客户名称”(即属性值)。 我想通过检查属性值“客户名称”来读取名称,我应该得到结果为“罗伯特先生

【问题讨论】:

标签: c# xml xmlnode xml-attribute


【解决方案1】:

你需要使用:

string test = objXmlMain.SelectSingleNode("Customer/Type/Row/C[@H='Customer Name']").Value;

Customer/Type/Row/C/@H xpath 查询选择 H 属性本身,.Value 返回该属性的文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 2012-08-13
    • 1970-01-01
    相关资源
    最近更新 更多