Linq to XML 未得到结果

发布于 2024-12-10 16:13:14 字数 1478 浏览 0 评论 0原文

我正在使用 linq to xml 创建结构列表。

linq 路径找不到概念元素。我已经尝试过各种形式,在我放弃并使用 xpath 之前,我希望有人可以向我展示 linq 方式。谢谢

xml

<response xmlns="http://www.domain.com/api">
  <about>
    <requestId>E9B73CA1F16A670C966BE2BABD3B2B22</requestId>
    <docId>09E167D994E00B0F511781C40B85AEC3</docId>
    <systemType>concept</systemType>
    <configId>odp_2007_l1_1.7k</configId>
    <contentType>text/plain</contentType>
    <contentDigest>09E167D994E00B0F511781C40B85AEC3</contentDigest>
    <requestDate>2011-10-18T09:51:28+00:00</requestDate>
    <systemVersion>2.1</systemVersion>
  </about>
  <conceptExtractor>
    <conceptExtractorResponse>
      <concepts>
        <concept weight="0.010466908" label="hell"/>
      </concepts>
    </conceptExtractorResponse>
  </conceptExtractor>
</response>

这是我所拥有的

public struct conceptweight
{
    public string concept { get; set; }
    public string weight { get; set; }
}

List<conceptweight> list = (from c
  in d.Descendants("response")
      .Descendants("conceptExtractor")
      .Descendants("conceptExtractorResponse")
      .Descendants("concepts")
  select new conceptweight()
         {
           concept = c.Attribute("label").Value,
           weight = c.Attribute("weight").Value
         }).ToList();

I am creating a List of structs using linq to xml.

The linq path does not find the concept elements. I have tried various formulations of this and before I give up and use xpath I am hoping someone can show me the linq way. thanks

Here is the xml

<response xmlns="http://www.domain.com/api">
  <about>
    <requestId>E9B73CA1F16A670C966BE2BABD3B2B22</requestId>
    <docId>09E167D994E00B0F511781C40B85AEC3</docId>
    <systemType>concept</systemType>
    <configId>odp_2007_l1_1.7k</configId>
    <contentType>text/plain</contentType>
    <contentDigest>09E167D994E00B0F511781C40B85AEC3</contentDigest>
    <requestDate>2011-10-18T09:51:28+00:00</requestDate>
    <systemVersion>2.1</systemVersion>
  </about>
  <conceptExtractor>
    <conceptExtractorResponse>
      <concepts>
        <concept weight="0.010466908" label="hell"/>
      </concepts>
    </conceptExtractorResponse>
  </conceptExtractor>
</response>

here is what I have

public struct conceptweight
{
    public string concept { get; set; }
    public string weight { get; set; }
}

List<conceptweight> list = (from c
  in d.Descendants("response")
      .Descendants("conceptExtractor")
      .Descendants("conceptExtractorResponse")
      .Descendants("concepts")
  select new conceptweight()
         {
           concept = c.Attribute("label").Value,
           weight = c.Attribute("weight").Value
         }).ToList();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-12-17 16:13:14

您忘记了根元素中默认的名称空间。试试这个:

// Note: names changed to follow conventions, and now a class
// rather than a struct. Mutable structs are evil.
public class ConceptWeight
{
    public string Concept { get; set; }
    // Type changed to reflect the natural data type
    public double Weight { get; set; }
}

XNamespace ns = "http://www.domain.com/api";

// No need to traverse the path all the way, unless there are other "concepts"
// elements you want to ignore. Note the use of ns here.
var list = d.Descendants(ns + "concepts")
            .Select(c => new ConceptWeight
                    {
                        Concept = (string) c.Attribute("label"),
                        Weight = (double) c.Attribute("weight"),
                    })
            .ToList();

我在这里没有使用查询表达式,因为它没有添加任何值 - 您只是执行 from x in y select z ,这可以通过 Select 更简单地表达 扩展方法。

You've forgotten the namespace, which is defaulted in the root element. Try this:

// Note: names changed to follow conventions, and now a class
// rather than a struct. Mutable structs are evil.
public class ConceptWeight
{
    public string Concept { get; set; }
    // Type changed to reflect the natural data type
    public double Weight { get; set; }
}

XNamespace ns = "http://www.domain.com/api";

// No need to traverse the path all the way, unless there are other "concepts"
// elements you want to ignore. Note the use of ns here.
var list = d.Descendants(ns + "concepts")
            .Select(c => new ConceptWeight
                    {
                        Concept = (string) c.Attribute("label"),
                        Weight = (double) c.Attribute("weight"),
                    })
            .ToList();

I haven't used a query expression here as it wasn't adding any value - you were only doing from x in y select z, which is more simply expressed via the Select extension method.

星軌x 2024-12-17 16:13:14
XNamespace n = "http://www.domain.com/api";
List<conceptweight> list = (from c in d.Elements(n + "response").Elements(n + "conceptExtractor").Elements(n + "conceptExtractorResponse").Elements(n + "concepts").Elements(n + "concept")
                            select new conceptweight
                            {
                                concept = c.Attribute("label").Value,
                                weight = c.Attribute("weight").Value
                            }).ToList();

您忘记了命名空间和最后一个元素(concept)。啊,如果您进行内联初始化,则不需要 new ConceptWeight 中的 () 括号。 Ah 和后代将遍历元素的所有“级别”。如果您想“手动”遍历它,请使用 Elements

XNamespace n = "http://www.domain.com/api";
List<conceptweight> list = (from c in d.Elements(n + "response").Elements(n + "conceptExtractor").Elements(n + "conceptExtractorResponse").Elements(n + "concepts").Elements(n + "concept")
                            select new conceptweight
                            {
                                concept = c.Attribute("label").Value,
                                weight = c.Attribute("weight").Value
                            }).ToList();

You forgot the namespace and the last element (concept). Ah and you don't need the () brackets in new conceptweight if you do the inline initialization. Ah and Descendants will traverse all the "levels" of elements. If you want to traverse it "manually", use Elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文