C#——LINQ to XML(使用 Descendants 方法查找单个子代)

xml位于命名空间中时查找

复制代码
        static void Main(string[] args)
        {
            XElement root = XElement.Parse(@"<aw:Root xmlns:aw='http://www.efun.com'>
  <aw:Child1>
    <aw:GrandChild1>GC1 Value</aw:GrandChild1>
  </aw:Child1>
  <aw:Child2>
    <aw:GrandChild2>GC2 Value</aw:GrandChild2>
  </aw:Child2>
  <aw:Child3>
    <aw:GrandChild3>GC3 Value</aw:GrandChild3>
  </aw:Child3>
  <aw:Child4>
    <aw:GrandChild4>GC4 Value</aw:GrandChild4>
  </aw:Child4>
</aw:Root>");
            XNamespace aw = "http://www.efun.com";
            string grandChild3 = (string)
                (from el in root.Descendants(aw + "GrandChild3")
                 select el).First();
            Console.WriteLine(grandChild3);
        }
复制代码

输出结果:

GC3 Value

 

posted @   echo-efun  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示