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.

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

ugh... simple solutions are the worst to spot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.