使用包含命名空间的 XPath 进行 C# XML 查询

问题描述 投票:0回答:1
c# xml xpath xml-namespaces
1个回答
1
投票

最好用LINQ to XML

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<SomeEntity xmlns='http://www.example.com/Schemas/SomeEntity/2023/01'>
          <Child>
            <TextValue>Some text</TextValue>
          </Child>
        </SomeEntity>");
    
    XNamespace ns = xdoc.Root.GetDefaultNamespace();

    string TextValue = xdoc.Descendants(ns + "TextValue")?.FirstOrDefault().Value;
    Console.WriteLine("TextValue='{0}'", TextValue);
}
© www.soinside.com 2019 - 2024. All rights reserved.