簡體   English   中英

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

[英]Extracting specific strings from a text file

我有一個帶有多個 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>

每個 ViewBox 都有一個位於雙引號內的鍵,如上所示,它也有 Path 標簽。

問題是我可以提取路徑標簽的數據屬性之間的視圖框鍵和數據,並使用正則表達式將其作為鍵和值放入(字符串,字符串)字典中嗎?

如果是,那么如果有人提供與此匹配的正則表達式會很好,否則除了正則表達式之外還有其他方法嗎?

實現此目的的一種方法是解析 xml 文檔,然后找到所需的元素/標簽。 請參閱以下代碼段。

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

這是 output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM