samedi 9 mai 2015

How do I get the full name of the Xml Node

I have a sample xml file that looks like this.

    <Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://ift.tt/o66D3f"
    xmlns:x="http://ift.tt/mPTqtT"
    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!

Aucun commentaire:

Enregistrer un commentaire