简体   繁体   English

在XElement内部获取价值

[英]Get value inside XElement

I've been trying forever using Descendants, Elements and attributes to retrieve data from an XML file that I'm writing to save metadata. 我一直在尝试使用后代,元素和属性从我正在编写的XML文件中检索数据以保存元数据。 I can't at the moment and it's blocking my work. 我暂时无法,这阻碍了我的工作。 When I was using Linq to XML, I was yielding no value whatsoever and I couldn't under why happening. 当我使用Linq到XML时,我没有任何价值,我也不知道为什么会这样。

Quick look into the xml file 快速查看xml文件

    <ImageMetadata>
        <ColorHistogram>
            <Bin value="45861"/>
            <Bin value="31989"/>
        </ColorHistogram/>
        <FaceLocations>
            <FacePosition Y="379" X="205"/>
            <FacePosition Y="366" X="372"/>
        </FaceLocations>
    </ImageMetadata>

I've tried different solutions. 我尝试了不同的解决方案。 At first, I only had an XElement called BinValue instead of the tag Bin with the attribute value which lead to this code: 最初,我只有一个名为BinValue的XElement,而不是带有属性值的标记Bin,该属性导致了以下代码:

  //Yielding no results

  from elements in doc.Descendants()
  let element = elements.Element("BinValue")
  select (long)element;

Then after getting mad at LINQ to XML, I changed the structure a bit of my document to have a tag and an attribute. 然后,在对LINQ to XML感到不满之后,我将文档的结构进行了一些更改,使其具有标签和属性。 But this have no effect whatsoever. 但这没有任何作用。

var bins = XElement.Load(dbMetadata)
                .Descendants("Bin")
                .Select(e => e.Attribute("value").Value);
// which gives me : System.ArgumentException: 'Illegal characters in path.'

Getting values from xelement 从xelement获取价值

My use case has you could have gather from the xml structure is the following: creating metadata of an image file. 您可以从xml结构中收集到以下用例:创建图像文件的元数据。 That portion looks pretty solid with OpenCV and that's not my issue. 这部分与OpenCV看起来相当牢固,这不是我的问题。 Maybe in order to get more feedback on my issue, it'd be relevant to add the code I was using to build my XML document. 也许为了获得关于我的问题的更多反馈,添加我用来构建XML文档的代码是很重要的。

The portion that computes data on an image was done using F#. 使用F#完成在图像上计算数据的部分。 The part that creates the xml document was done using C#. 创建xml文档的部分是使用C#完成的。 Because of that, I'm going to create two gists to share my code. 因此,我将创建两个要点来共享我的代码。 Please, remember to add the Emgu OpenCV nugget package in your solutions. 请记住,在您的解决方案中添加Emgu OpenCV块软件包。

  1. Image Analyzer - F# 图像分析仪-F#
  2. XMLDocumentParser - C# XMLDocumentParser-C#

**Use any two local jpg files in order to run the F# code that will generate the metadata ! **使用任何两个本地jpg文件以运行将生成元数据的F#代码!

**If possible, I'd like a way to retrieve the data using LINQ to XML. **如果可能的话,我想使用LINQ to XML检索数据的方法。 For both ColorHistogram and FaceLocations 对于ColorHistogram和FaceLocations

UPDATE1 更新1

I was asked in the comments to show the state of the xml file when my problems occurred. 出现问题时,我在注释中要求我显示xml文件的状态。 You can find it below: 您可以在下面找到它:

Metadata file 元数据文件

Try following : 尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication49
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("ImageMetadata").Select(x => new
            {
                colorHistograms = x.Descendants("ColorHistogram").Select(y => new
                {
                    bin = y.Elements("Bin").Select(z => new
                    {
                        value = (int)z.Attribute("value")
                    }).ToList()
                }).FirstOrDefault(),
                faceLocations = x.Descendants("FaceLocations").Select(y => new
                {
                    facePosition = y.Elements("FacePosition").Select(z => new
                    {
                        X = (int)z.Attribute("X"),
                        Y = (int)z.Attribute("Y")
                    }).ToList()
                }).FirstOrDefault()

            }).FirstOrDefault();


        }
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM