0

I have a sample xml file that looks like this.

    <Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel>
        <TextBlock Text="First Text" Margin="5"/>
        <Label Content="Second Text" HorizontalAlignment="Center"/>
        <TextBox Text="Third Text"/>
        <GroupBox Header="Fourth Text">
            Fifth Text
            that extends to another line.
        </GroupBox>
        <Button Content="Sixth Text"/>
        <Frame Content="&lt;Seventh Text&gt;"></Frame>
        <ComboBox>
            Eighth Text</ComboBox>
        <Label Content="{Binding LabelText}" HorizontalAlignment="Center"/>
    </StackPanel>
</Grid>

My output file looks like this:

    (4)Title="MainWindow"
    (7)Text="First Text"
    (8)Content="Second Text"
    (9)Text="Third Text"
    (10)Header="Fourth Text"
    (11) Fifth Text                that extends to another line.
    (14)Content="Sixth Text"
    (15)Content="&lt;Seventh Text&gt;"
    (17) Eighth Text

This is mostly what I want. However, for some reason I am only getting "Title" and "Text" and "Content" so forth. But I want it to to print out "TextBlock Text" and "Label Content" and "TextBox Text" and "Button Content" and so forth. I am using an XmlTextReader but I can't seem to find any support for that to print that out. reader.Name simply just prints out what I already have.
Here is my Code:

    public void ParseXml(String filename)
    {
        XmlTextReader reader = new XmlTextReader(filename);
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    for (int i = 0; i < reader.AttributeCount; i++)
                    {
                        reader.MoveToAttribute(i);
                        if (reader.LineNumber < 4)
                        {
                            continue;
                        }
                        //WriteLine(Path.GetFullPath(filename));
                        if(reader.Name != "Width" && reader.Name != "Height" && reader.Name != "Margin"
                            && reader.Name != "HorizontalAlignment")
                            WriteLine("(" + reader.LineNumber + ")" + reader.ReadOuterXml());                         
                    }
                    break;
                case XmlNodeType.Text:
                    WriteLine("(" + (reader.LineNumber + 1) + ") " + reader.Value.Replace("\r\n","").Trim());
                    break;
                case XmlNodeType.EndElement:
                    break;
            }
        }
        reader.Close();
    }

Thank you!

  • possible duplicate of [How to parse this specific XML to grab the FULLNAME node, in C#?](http://stackoverflow.com/questions/28749222/how-to-parse-this-specific-xml-to-grab-the-fullname-node-in-c) – MethodMan May 08 '15 at 19:33
  • 3
    You'd need to get the reader.Name before you MoveToAttribute. Is there any reason you're reading this way rather than using LINQ to XML? – Charles Mager May 08 '15 at 19:38
  • Not particularly, I couldn't quite get a handle on the LINQ to XML. If you could possibly post an example of how I would use Linq to xml that would be great – PaperWings May 08 '15 at 20:10
  • Also, +1 because that solved my exact problem.. Thank you! If you want, feel free though to show to me how do that in LINQ to Xml as I am very interested. I know that it's newer and I believe more popular. – PaperWings May 08 '15 at 20:17

1 Answers1

0

As I hinted in the comments, you need to get the result of reader.Name while the reader is still on the element - before you move it by calling reader.MoveToAttribute(i).

Unless you have a particular reason (such as performance or a very large file), it would be a lot easier to use a higher level API such as LINQ to XML.

A simple implementation might look like this:

var document = XDocument.Load(filename, LoadOptions.SetLineInfo);

var excludedAttributes = new HashSet<XName>
{
    "{http://schemas.microsoft.com/winfx/2006/xaml}Class",
    "Width",
    "Height",
    "Margin",
    "HorizontalAlignment"
};

foreach (var element in document.Descendants())
{
    IXmlLineInfo lineInfo = element;

    var sb = new StringBuilder();

    sb.AppendFormat("({0}) ", lineInfo.LineNumber);
    sb.Append(element.Name.LocalName);

    var outputAttributes = element.Attributes()
        .Where(a => !a.IsNamespaceDeclaration && !excludedAttributes.Contains(a.Name));

    foreach (var attribute in outputAttributes)
    {
        sb.AppendFormat(" {0}=\"{1}\"", attribute.Name, attribute.Value);
    }

    if (element.Nodes().OfType<XText>().Any())                
    {
        sb.Append(" ");
        sb.Append(element.Value.Replace("\n", string.Empty).Trim());
    }

    WriteLine(sb.ToString());
}
Charles Mager
  • 25,735
  • 2
  • 35
  • 45