C#-如何获取子元素val,其中另一个子元素等于val

时间:2018-06-30 12:03:51

标签: c# xml

我正在尝试获取另一个子元素值等于某个值的子元素的值,

例如,我有这个xml文件:

<CATALOG>
    <game>
        <name>Assassins Creed Origins</name>
        <picture>pic1</picture>
        <torrent>file1</torrent>
    </game>
    <game>
        <name>mylifeisdone</name>
        <picture>pic2</picture>
        <torrent>file2</torrent>
    </game>
</CATALOG>

我想获取picture等于name的{​​{1}}值

2 个答案:

答案 0 :(得分:1)

使用Xml Linq:

-

答案 1 :(得分:0)

我想到的最简单的方法是使用keep_prob

# array of ones
data=np.ones((3,4), dtype=np.float32)

keep_prob = tf.placeholder(tf.float32)
drop = tf.nn.dropout(data, keep_prob=keep_prob)

sess = tf. InteractiveSession()

print(sess.run(drop, feed_dict={keep_prob: 0.5}))
# >>> [[0. 2. 0. 2.]
#      [0. 2. 0. 0.]
#      [2. 2. 2. 2.]]

print(sess.run(drop, feed_dict={keep_prob: 1.0}))
# >>> [[1. 1. 1. 1.]
#      [1. 1. 1. 1.]
#      [1. 1. 1. 1.]]

首先获取所有元素“游戏”,然后搜索第一个元素,其中XDocument元素的值为“ mylifeisdone”;之后,它将检索“图片”元素的值。

注意:您可能需要命名空间XDocument doc = XDocument.Parse(@" <CATALOG> <game> <name>Assassins Creed Origins</name> <picture>pic1</picture> <torrent>file1</torrent> </game> <game> <name>mylifeisdone</name> <picture>pic2</picture> <torrent>file2</torrent> </game> </CATALOG>"); var picture = doc.Descendants("game") .First(g => g.Element("name").Value == "mylifeisdone") .Element("picture").Value; ,如果要从文件中读取XML,请使用name