このようなサンプルxmlファイルがあります。
<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="<Seventh Text>"></Frame>
<ComboBox>
Eighth Text</ComboBox>
<Label Content="{Binding LabelText}" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
私の出力ファイルは次のようになります。
(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="<Seventh Text>"
(17) Eighth Text
これは主に私が欲しいものです。ただし、何らかの理由で「タイトル」と「テキスト」と「コンテンツ」などしか取得できません。しかし、「TextBlock Text」と「Label Content」と「TextBox Text」と「Button Content」などを印刷したいのです。私は XmlTextReader を使用していますが、それを印刷するためのサポートが見つからないようです。reader.Name は、既に持っているものを単に出力するだけです。
ここに私のコードがあります:
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();
}
ありがとうございました!