English 中文(简体)
• 如何从XDocument的节点中获取一个内容,在这些节点中,另外两个要素是:
原标题:How to get an element from a node in XDocument where two other elements are known

I mumped (again) with an 误解 of XDocument/Linq. 关于下文的XML,我有<代码>名称En和-provinceCode,作为我的代码中的变量。 我试图确定<代码>代码(例如,,因为我有另外两个要素。 The provinceCode and NameEn joint are separate in the XML (noplica).

<siteList>
  <site code="s0000001">
    <nameEn>Edmonton</nameEn>
    <nameFr>Edmonton</nameFr>
    <provinceCode>AB</provinceCode>
  </site>
  <site code="s0000002">
    <nameEn>Algonquin Park</nameEn>
    <nameFr>Parc Algonquin</nameFr>
    <provinceCode>ON</provinceCode>
  </site>
...
</siteList>

Here s the code I m trying (my XML is in the "loaded" XDocument:

selectedProvince = "ON";
selectedCity = "Algonquin Park";      
strSiteCode = loaded.Descendants("site")
    .Where(x => x.Element("provinceCode").Value == selectedProvince)
    .Where(x => x.Element("nameEn").Value == selectedCity)
    .Select(x => x.Element("code").Value)
    .ToString();
strNameFR = loaded.Descendants("site")
    .Where(x => x.Element("provinceCode").Value == selectedProvince)
    .Where (x => x.Element("nameEn").Value == selectedCity)
    .Select(x => x.Element("nameFr").Value)
    .ToString();

The string strSiteCode returns: System.Linq.Enumerable+WhereSelectEnumerableIterator2[System.Xml.Linq.XElement,System.String]andstrNameFRreturns""`.

I can t figure out what the working code should look like. Thanks for any help.

Doug

最佳回答

我或许会把它照做:

  • get the list of matching <site> nodes once
  • iterate over all matches (typically only one)
  • get the code attribute and nameFr element from those matching items

法典希望:

// determine the matching list of <site> nodes ...
var selectedSites = loaded
                       .Descendants("site")
                       .Where(x => x.Element("provinceCode").Value == selectedProvince)
                       .Where(x => x.Element("nameEn").Value == selectedCity);

// iterate over all matching <site> nodes
foreach (var site in selectedSites)
{
    // grab the code attribute and nameFr element from <site> node
    var siteCode = site.Attribute("code").Value;
    var nameFR = site.Element("nameFr").Value;
}
问题回答

提 出

var result = loaded.Descendants("site")
    .Where(x => (x.Element("provinceCode").Value == selectedProvince) &&
                 (x.Element("nameEn").Value == selectedCity) )
    .Select(x => x.Element("code").Value)
    .SingleOrDefault();

if (result != null)
{
    strSiteCode = result.ToString();
}

<代码>选择(>>称收复(在你看来,只有一元)。 因此,请打电话<代码>SingleOrDefault()(或Single(<>>)。 另外,我删除了第二个<代码>(),并列入了第一个<代码>()的条件。

考虑到“x.Element(......)”方法有可能使“Value”(......)”法失效,因此获得“Value”(......)将造成无效。 假设您的xml不得always 各省或省。 如果是的话,你就没有问题了,但你不想在释放法中排除这种可能。 下面解决了无效的问题。

var site = loaded
    .Descendants("site")
    .FirstOrDefault(x => (string)x.Element("provinceCode") == selectedProvince &&
                    x => (string)x.Element("nameEn") == selectedCity);    

if (site == null)
{
    return
}

var siteCode = (string)site.Attribute("code");
var nameFr = (string)site.Element("nameFr");




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签