简体   繁体   English

从文本文件中提取特定字符串

[英]Extracting specific strings from a text file

I have a text file (wpf resource dictionary) with multiple tags of Viewboxes like below:我有一个带有多个 Viewboxes 标记的文本文件(wpf 资源字典),如下所示:

<Viewbox x:Shared="false" x:Key="Bookmarks Upload">
        <Grid Width="48" Height="48" Visibility="Visible">
            <Grid Visibility="Visible">
                <Rectangle Fill="#FF000000" Visibility="Visible"/>
                <Ellipse Fill="#FF000000" Visibility="Collapsed"/>
                <Path Data="M50.5,4.7500001C25.232973,4.75 4.75,25.232973 4.7500001,50.5 4.75,75.767029 25.232973,96.25 50.5,96.25 75.767029,96.25 96.25,75.767029 96.25,50.5 96.25,25.232973 75.767029,4.75 50.5,4.7500001z M50.5,0C78.390381,0 101,22.609621 101,50.5 101,78.390381 78.390381,101 50.5,101 22.609621,101 0,78.390381 0,50.5 0,22.609621 22.609621,0 50.5,0z" Stretch="Fill" Fill="#FF000000" Visibility="Collapsed"/>
            </Grid>
            <Path Data="M3.8820142,3.2789917L6.353996,3.2789917C6.1570055,3.9109497 6.0510179,4.5819702 6.0510179,5.2769775 6.0510179,8.9879761 9.0699961,12.008972 12.780991,12.008972 13.188003,12.008972 13.585006,11.966003 13.973005,11.896973L13.973005,32 6.8759987,24.015991 0,32 0,7.1609497C-1.8273931E-07,5.0180054,1.7380044,3.2789917,3.8820142,3.2789917z M12.886002,1.7340088L9.896991,5.1829834 11.627,5.1829834 11.627,8.3859863 14.272993,8.3859863 14.272993,5.1829834 15.878002,5.1829834z M12.780991,0C15.695996,0 18.058999,2.3619995 18.058999,5.2769775 18.058999,8.1929932 15.695996,10.554993 12.780991,10.554993 9.8669922,10.554993 7.504019,8.1929932 7.504019,5.2769775 7.504019,2.3619995 9.8669922,0 12.780991,0z" Stretch="Uniform" Fill="#FFFFFFFF" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
                <Path.RenderTransform>
                    <TransformGroup>
                        <TransformGroup.Children>
                            <RotateTransform Angle="0"/>
                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                        </TransformGroup.Children>
                    </TransformGroup>
                </Path.RenderTransform>
            </Path>
        </Grid>
    </Viewbox>

every ViewBox has a key which is inside double quotation as shown above and it has Path tags as well.每个 ViewBox 都有一个位于双引号内的键,如上所示,它也有 Path 标签。

the question is can I extract the viewbox keys and data between the data attribute of path tags and put it into a Dictionary of (string, string) as key and value using Regex?问题是我可以提取路径标签的数据属性之间的视图框键和数据,并使用正则表达式将其作为键和值放入(字符串,字符串)字典中吗?

if yes then it would be good if someone provides the regex that matches this otherwise is there any other way around doing this other than Regex?如果是,那么如果有人提供与此匹配的正则表达式会很好,否则除了正则表达式之外还有其他方法吗?

One way to achieve this is to parse the xml document and then find the required elements/tags you need.实现此目的的一种方法是解析 xml 文档,然后找到所需的元素/标签。 Refer the below snippet.请参阅以下代码段。

filePath variable contains location of your xml file filePath变量包含 xml 文件的位置

var extractedData = new Dictionary<string, string>();
var xmlObject = XElement.Load(filePath);
var x = xmlObject.GetNamespaceOfPrefix("x");
var viewBoxElemets = xmlObject.Elements("Viewbox").ToList();
foreach (var viewboxElement in viewBoxElemets)
{
    var viewBoxElementKey = viewboxElement.Attribute(x + "Key").Value;
    var pathElement = viewboxElement.Descendants().FirstOrDefault(x => x.Name == "Path");
    var pathDataValue = pathElement.Attribute("Data").Value;
    extractedData.Add(viewBoxElementKey, pathDataValue);
}

Here's the output:这是 output:

在此处输入图像描述

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

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