简体   繁体   中英

Extracting specific strings from a text file

I have a text file (wpf resource dictionary) with multiple tags of Viewboxes like below:

<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.

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. Refer the below snippet.

filePath variable contains location of your xml file

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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