如何使用LINQ to XML访问列中的XML数据?

时间:2021-10-21 14:31:20

I have this XML in a column in my table:

我在表格的列中有这个XML:

<keywords>
  <keyword name="First Name" value="|FIRSTNAME|" display="Jack" />
  <keyword name="Last Name" value="|LASTNAME|" display="Jones" />
  <keyword name="City" value="|CITY|" display="Anytown" />
  <keyword name="State" value="|STATE|" display="MD" />
</keywords>

I'm getting a record out of that table using LINQ to SQL via this:

我通过这个使用LINQ to SQL获取该表的记录:

GeneratedArticle ga = db.GeneratedArticles.Single(p => p.GeneratedArticleId == generatedArticleId);

That works, I get my GeneratedArticle object just fine.

这有效,我得到我的GeneratedArticle对象就好了。

I'd like to walk through the data in the ArticleKeywords field, which is XML. I started doing this:

我想浏览ArticleKeywords字段中的数据,即XML。我开始这样做:

var keywords = from k in ga.ArticleKeywords.Elements("Keywords")
                    select k;

            foreach (var keyword in keywords)
            {
             //what goes here?   
            }

I'm not 100% sure that I'm getting that data correctly. I need help with the proper syntax to get the value and display out of my XML field.

我不是100%确定我正确地获得了这些数据。我需要帮助使用正确的语法来获取值并显示出我的XML字段。

3 个解决方案

#1


2  

Here is a sample code:

这是一个示例代码:

To read keywords we need to call Elements("keyword") not Elements("keywords") since keywords is a root node.

要读取关键字,我们需要调用Elements(“keyword”)而不是Elements(“keywords”),因为关键字是根节点。

// IEnumerable sequence with keywords data
var keywords = from kw in ga.ArticleKeywords.Elements("keyword")
               select new {
                   Name = (string)kw.Attribute("name"),
                   Value = (string)kw.Attribute("value"),
                   Display = (string)kw.Attribute("display")
               };


foreach (var keyword in keywords)
{
    var kw = "Name: " + keyword.Name + 
             " Value: " + keyword.Value + 
             " Display: " + keyword.Display;
    Console.WriteLine(kw);
}

You can get attribute value using foo.Attribute("bar").Value, but this method would throw exception if attribute is missing. Safe way to get attribute value is (string)foo.Attribute("bar") - it will give you null if attribute is missing

您可以使用foo.Attribute(“bar”)获取属性值。值,但如果缺少属性,此方法将抛出异常。获取属性值的安全方法是(string)foo.Attribute(“bar”) - 如果缺少属性,它将为null

#2


3  

Once again I am amazed that people don't even try their answers and people still up vote when they don't work. The .Elements will get the list of elements at the current root, which is not a keyword.

我再次感到惊讶的是,人们甚至没有尝试他们的答案,人们在他们不工作时仍然投票。 .Elements将获取当前根目录中的元素列表,该列表不是关键字。

Also the one using .Attributes["X"] does not even compile you need to use () of course again it would be operating on each instance of "keywords" not "keyword"

另外使用.Attributes [“X”]的人甚至不编译你需要使用()当然它会在每个“关键字”实例上运行而不是“关键字”

You could use

你可以用

var keywords = from kw in ga.ArticleKeywords.Element("keywords").Elements()

or

var keywords = from kw in ga.ArticleKeywords.Element("keywords").Elements("keyword")

or (this will get all the keyword elements regardless of level)

或(这将获得所有关键字元素,无论级别如何)

var keywords = from kw in ga.ArticleKeywords.Descendants("keyword")

#3


0  

I would think something like this would work

我认为这样的事情会起作用

var keywordData = from k in ga.ArticleKeywords.Elements("Keywords")
                  select new { Value = k.Attributes["value"].Value,
                               Display = k.Attributes["display"].Value};

This would give you an IEnumerable of an anonymous type containing a Value and a Display property. Depending on what your doing you could easily replace the anonymous type with a concrete type.

这将为您提供包含Value和Display属性的IEnumerable匿名类型。根据您的操作,您可以轻松地使用具体类型替换匿名类型。

#1


2  

Here is a sample code:

这是一个示例代码:

To read keywords we need to call Elements("keyword") not Elements("keywords") since keywords is a root node.

要读取关键字,我们需要调用Elements(“keyword”)而不是Elements(“keywords”),因为关键字是根节点。

// IEnumerable sequence with keywords data
var keywords = from kw in ga.ArticleKeywords.Elements("keyword")
               select new {
                   Name = (string)kw.Attribute("name"),
                   Value = (string)kw.Attribute("value"),
                   Display = (string)kw.Attribute("display")
               };


foreach (var keyword in keywords)
{
    var kw = "Name: " + keyword.Name + 
             " Value: " + keyword.Value + 
             " Display: " + keyword.Display;
    Console.WriteLine(kw);
}

You can get attribute value using foo.Attribute("bar").Value, but this method would throw exception if attribute is missing. Safe way to get attribute value is (string)foo.Attribute("bar") - it will give you null if attribute is missing

您可以使用foo.Attribute(“bar”)获取属性值。值,但如果缺少属性,此方法将抛出异常。获取属性值的安全方法是(string)foo.Attribute(“bar”) - 如果缺少属性,它将为null

#2


3  

Once again I am amazed that people don't even try their answers and people still up vote when they don't work. The .Elements will get the list of elements at the current root, which is not a keyword.

我再次感到惊讶的是,人们甚至没有尝试他们的答案,人们在他们不工作时仍然投票。 .Elements将获取当前根目录中的元素列表,该列表不是关键字。

Also the one using .Attributes["X"] does not even compile you need to use () of course again it would be operating on each instance of "keywords" not "keyword"

另外使用.Attributes [“X”]的人甚至不编译你需要使用()当然它会在每个“关键字”实例上运行而不是“关键字”

You could use

你可以用

var keywords = from kw in ga.ArticleKeywords.Element("keywords").Elements()

or

var keywords = from kw in ga.ArticleKeywords.Element("keywords").Elements("keyword")

or (this will get all the keyword elements regardless of level)

或(这将获得所有关键字元素,无论级别如何)

var keywords = from kw in ga.ArticleKeywords.Descendants("keyword")

#3


0  

I would think something like this would work

我认为这样的事情会起作用

var keywordData = from k in ga.ArticleKeywords.Elements("Keywords")
                  select new { Value = k.Attributes["value"].Value,
                               Display = k.Attributes["display"].Value};

This would give you an IEnumerable of an anonymous type containing a Value and a Display property. Depending on what your doing you could easily replace the anonymous type with a concrete type.

这将为您提供包含Value和Display属性的IEnumerable匿名类型。根据您的操作,您可以轻松地使用具体类型替换匿名类型。