2

Here is the xml:

<_text_column min_width="0" max_width="2031051">
    <PF_Para_Base align="center">
        <_char data_tag="PricesGoodText" font_size="35270" bold="true" italic="false" font_name="/ITC Franklin Gothic Demi" text_color="White">PricesGoodText</_char>
    </PF_Para_Base>
</_text_column>

I am opening and appending a root to the file because I was getting a multiple root error with the file

using (var fs = new StreamReader(fullFileName))
using (var xr = XmlReader.Create(fs, settings))
{
    while (xr.Read())
    {
        if (xr.NodeType == XmlNodeType.Element)
        {
            rootElement.Add(XElement.Load(xr.ReadSubtree()));
        }
    }
}

var attr = rootElement.Elements("char").Attribute("data_tag");

I need to get the attribute data_tag out of the node _char. It comes back as null.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
tscLegend
  • 76
  • 11

1 Answers1

3

rootElement.Elements("_char").Attributes("data_tag"); is wrong as Elements read just the direct children, you should use Descendants:

rootElement.Descendants("_char").Select(c => c.Attribute("data_tag").Value); 

Check this SO question: What is the difference between Linq to XML Descendants and Elements

Community
  • 1
  • 1
w.b
  • 11,026
  • 5
  • 30
  • 49