59

我有这个代码:

/*string theXml =
@"<Response xmlns=""http://myvalue.com""><Result xmlns:a=""http://schemas.datacontract.org/2004/07/My.Namespace"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><a:TheBool>true</a:TheBool><a:TheId>1</a:TheId></Result></Response>";*/

string theXml = @"<Response><Result><TheBool>true</TheBool><TheId>1</TheId></Result></Response>";

XDocument xmlElements = XDocument.Parse(theXml);

var elements = from data in xmlElements.Descendants("Result")
               select new {
                            TheBool = (bool)data.Element("TheBool"),
                            TheId = (int)data.Element("TheId"),
                          };

foreach (var element in elements)
{
    Console.WriteLine(element.TheBool);
    Console.WriteLine(element.TheId);
}

当我为 theXml 使用第一个值时,结果为空,而使用第二个值时,我有很好的值......

如何使用带有 xmlns 值的 Linq to Xml?

4

4 回答 4

92

LINQ to XML 方法喜欢DescendantsElement以 aXName作为参数。有一个转换stringXName你自动发生。您可以通过在and调用XNamespace中的字符串之前添加一个来解决此问题。请注意,因为您有 2 个不同的命名空间在工作。DescendantsElement


string theXml =
                @"true1";

            //string theXml = @"true1";

    XDocument xmlElements = XDocument.Parse( theXml );
    XNamespace ns = "http://myvalue.com";
    XNamespace nsa = "http://schemas.datacontract.org/2004/07/My.Namespace";
    var elements = from data in xmlElements.Descendants( ns + "Result" )
          select new
                 {
                     TheBool = (bool) data.Element( nsa + "TheBool" ),
                     TheId = (int) data.Element( nsa + "TheId" ),
                 };

    foreach ( var element in elements )
    {
        Console.WriteLine( element.TheBool );
        Console.WriteLine( element.TheId );
    }

注意 ns inDescendants和 nsa in的使用Elements

于 2010-02-26T09:14:35.050 回答
26

您可以将带有命名空间的XName传递给Descendants()Element()。当您将字符串传递给 Descendants() 时,它会隐式转换为没有命名空间的 XName。

要在命名空间中创建 XName,您需要创建一个 XNamespace 并将其连接到元素 local-name(一个字符串)。

XNamespace ns = "http://myvalue.com";
XNamespace nsa = "http://schemas.datacontract.org/2004/07/My.Namespace";

var elements = from data in xmlElements.Descendants( ns + "Result")
                   select new
                              {
                                  TheBool = (bool)data.Element( nsa + "TheBool"),
                                  TheId = (int)data.Element( nsa + "TheId"),
                              };

还有一种简写形式,用于通过字符串的隐式转换来创建具有命名空间的 XName。

var elements = from data in xmlElements.Descendants("{http://myvalue.com}Result")
                   select new
                              {
                                  TheBool = (bool)data.Element("{http://schemas.datacontract.org/2004/07/My.Namespace}TheBool"),
                                  TheId = (int)data.Element("{http://schemas.datacontract.org/2004/07/My.Namespace}TheId"),
                              };

或者,您可以查询 XElement。名称.本地名称。

var elements = from data in xmlElements.Descendants()
                   where data.Name.LocalName == "Result"
于 2010-02-26T09:06:27.407 回答
3

我在 XML 文档的顶部列出了几个名称空间,我并不真正关心哪些元素来自哪个名称空间。我只想按名称获取元素。我已经写了这个扩展方法。

    /// <summary>
    /// A list of XElement descendent elements with the supplied local name (ignoring any namespace), or null if the element is not found.
    /// </summary>
    public static IEnumerable<XElement> FindDescendants(this XElement likeThis, string elementName) {
        var result = likeThis.Descendants().Where(ele=>ele.Name.LocalName==elementName);
        return result;
    }
于 2017-04-12T23:56:53.627 回答
0

我发现以下代码可以很好地用于在 VB.NET 中读取具有名称空间的属性:

MyXElement.Attribute(MyXElement.GetNamespaceOfPrefix("YOUR_NAMESPACE_HERE") + "YOUR_ATTRIB_NAME")

希望这对未来的人有所帮助。

于 2016-04-04T17:40:28.670 回答