繁体   English   中英

如何获得Xml节点的全名

[英]How do I get the full name of the Xml Node

我有一个示例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="&lt;Seventh Text&gt;"></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="&lt;Seventh Text&gt;"
    (17) Eighth Text

这主要是我想要的。 但是,由于某种原因,我只能得到“标题”,“文本”和“内容”等等。 但是我希望它打印出“ TextBlock文本”和“标签内容”以及“ TextBox文本”和“按钮内容”等等。 我正在使用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();
    }

谢谢!

正如我在评论中所暗示的那样,您需要在读者仍在元素上的同时获取reader.Name的结果- 通过调用reader.MoveToAttribute(i)移动之前

除非有特殊原因(例如性能或非常大的文件),否则使用更高级别的API(例如LINQ to XML)会容易得多。

一个简单的实现可能看起来像这样:

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());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM