如何从 C# 中的动态 XML 中读取特定值?

How to read specific values from a dynamic XML in C#?

我有一个项目需要程序以 XML 的形式流式传输元数据。我尝试了在网上找到的各种方法来遍历 XML 但我只设法打印出 XML.
的内部文本
我如何遍历 XML 并获取 tt:object“ObjectID”及其子“Gender”的值?

这是 XML 的示例。

<tt:MetadataStream
    xmlns:tt="http://www.onvif.org/ver10/schema"
    xmlns:ttr="https://www.onvif.org/ver20/analytics/radiometry"
    xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
    xmlns:tns1="http://www.onvif.org/ver10/topics"
    xmlns:tnssamsung="http://www.samsungcctv.com/2011/event/topics"
    xmlns:fc="http://www.onvif.org/ver20/analytics/humanface"
    xmlns:bd="http://www.onvif.org/ver20/analytics/humanbody">
    <tt:VideoAnalytics>
        <tt:Frame UtcTime="2021-09-11T09:09:21.816Z">
            <tt:Transformation>
                <tt:Translate x="-1.0" y="1.0"/>
                <tt:Scale x="0.000521" y="-0.000926"/>
            </tt:Transformation>
            <tt:Object ObjectId="733">
                <tt:Appearance>
                    <tt:Shape>
                        <tt:BoundingBox left="892.0" top="322.0" right="964.0" bottom="396.0"/>
                        <tt:CenterOfGravity x="928.0" y="359.0"/>
                    </tt:Shape>
                    <tt:Class>
                        <tt:Type Likelihood="0.3">Face</tt:Type>
                    </tt:Class>
                </tt:Appearance>
            </tt:Object>
            <tt:Object ObjectId="759">
                <tt:Appearance>
                    <tt:Shape>
                        <tt:BoundingBox left="3484.0" top="8.0" right="3836.0" bottom="616.0"/>
                        <tt:CenterOfGravity x="3660.0" y="312.0"/>
                    </tt:Shape>
                    <tt:Class>
                        <tt:Type Likelihood="0.9">Head</tt:Type>
                    </tt:Class>
                </tt:Appearance>
            </tt:Object>
            <tt:Object ObjectId="760">
                <tt:Appearance>
                    <tt:Shape>
                        <tt:BoundingBox left="3207.0" top="104.0" right="3839.0" bottom="1900.0"/>
                        <tt:CenterOfGravity x="3523.0" y="1002.0"/>
                    </tt:Shape>
                    <tt:ProximateObjects>
                        <tt:ProximateObject Id="0" Distance="0.000000"/>
                    </tt:ProximateObjects>
                    <tt:Class>
                        <tt:Type Likelihood="0.6">Human</tt:Type>
                    </tt:Class>
                </tt:Appearance>
            </tt:Object>
            <tt:Object ObjectId="761">
                <tt:Appearance>
                    <tt:Shape>
                        <tt:BoundingBox left="1208.0" top="283.0" right="1280.0" bottom="358.0"/>
                        <tt:CenterOfGravity x="1244.0" y="320.5"/>
                    </tt:Shape>
                    <tt:Class>
                        <tt:Type Likelihood="0.3">Face</tt:Type>
                    </tt:Class>
                </tt:Appearance>
            </tt:Object>
            <tt:Object ObjectId="758" Parent="760">
                <tt:Appearance>
                    <tt:Shape>
                        <tt:BoundingBox left="0.0" top="0.0" right="0.0" bottom="0.0"/>
                        <tt:CenterOfGravity x="0.0" y="0.0"/>
                    </tt:Shape>
                    <tt:Class>
                        <tt:Type Likelihood="0.4">Face</tt:Type>
                    </tt:Class>
                    <tt:HumanFace>
                        <fc:Gender>Male</fc:Gender>
                        <fc:AgeType>Adult</fc:AgeType>
                        <fc:Accessory>
                            <fc:Opticals>
                                <fc:Wear>false</fc:Wear>
                            </fc:Opticals>
                            <fc:Mask>
                                <fc:Wear>false</fc:Wear>
                            </fc:Mask>
                            <fc:Hat>
                                <fc:Wear>false</fc:Wear>
                            </fc:Hat>
                        </fc:Accessory>
                    </tt:HumanFace>
                </tt:Appearance>
            </tt:Object>
        </tt:Frame>
    </tt:VideoAnalytics>
</tt:MetadataStream>

感谢大家的帮助。我设法通过 LINQ to XML 查询获取了元素和值。