我想在xml文件中搜索元素值并从中获取特定的字符串。例如,我的xml文件中有以下元素值:
<Content>Shape="Rectangle" Tooltip="data" StrokeThickness="2" Tag="default" </Content>
现在我想阅读这个Content
并获取Tooltip
值作为我的结果字符串,这应该给我“数据”。我想要这样的东西:
string restuledString = Tooltip.value
怎么做?
更新
这是完整的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<Topologies>
<ConfigurationItems>
<ConfigurationItem Label="Default Tag">
<Attribute Name="display_lable" Label="Display Label" Type="clas java.lang.String" Value="Default Tag" />
<Attribute Name="MemorySize" Label="MemorySize" Type="class java.lang.Integer" Value="Default Tag" />
<Attribute Name="Description" Label="Description" Type="class java.lang.String" Value="SunOS" />
<Attribute Name="DiscoveredModel" Label="DiscoveredModel" Type="class java.lang.String" Value="Oracle Corporation sun4v SPARC T5-4" />
<X>289</X>
<Y>108.53999999999999</Y>
<Width>130</Width>
<Height>117</Height>
<ID>ed56efb4-6bdd-4dc7-8b79-db5e9b8484f1</ID>
<zIndex>0</zIndex>
<IsGroup>false</IsGroup>
<ParentID>00000000-0000-0000-0000-000000000000</ParentID>
<Content><Rectangle StrokeThickness="2" Tag="Default Thread Tag" ToolTip="data" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dd="clr-namespace:DiagramDesigner;assembly=ADAX_GUI"><Rectangle.Style><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="UIElement.RenderTransform"><Setter.Value><SkewTransform AngleX="-15" AngleY="0" CenterX="50" CenterY="25" /></Setter.Value></Setter><Setter Property="Shape.Fill"><Setter.Value><LinearGradientBrush StartPoint="0,0" EndPoint="0,1"><LinearGradientBrush.GradientStops><GradientStop Color="#FFFAFBE9" Offset="0" /><GradientStop Color="#FFFFA500" Offset="1" /></LinearGradientBrush.GradientStops></LinearGradientBrush></Setter.Value></Setter><Setter Property="Shape.Stroke"><Setter.Value><SolidColorBrush>#FFD69436</SolidColorBrush></Setter.Value></Setter><Setter Property="Shape.StrokeThickness"><Setter.Value><s:Double>1</s:Double></Setter.Value></Setter><Setter Property="Shape.StrokeLineJoin"><Setter.Value><x:Static Member="PenLineJoin.Round" /></Setter.Value></Setter><Setter Property="Shape.Stretch"><Setter.Value><x:Static Member="Stretch.Fill" /></Setter.Value></Setter><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>False</s:Boolean></Setter.Value></Setter><Setter Property="UIElement.SnapsToDevicePixels"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="Shape.StrokeDashArray"><Setter.Value><DoubleCollection>1.5 0.7</DoubleCollection></Setter.Value></Setter></Style></Rectangle.Style><dd:DesignerItem.DragThumbTemplate><ControlTemplate><Rectangle Name="ThreadShape" Width="250" Height="150"><Rectangle.Style><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.BasedOn><Style TargetType="Rectangle"><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="UIElement.RenderTransform"><Setter.Value><SkewTransform AngleX="-15" AngleY="0" CenterX="50" CenterY="25" /></Setter.Value></Setter><Setter Property="Shape.Fill"><Setter.Value><LinearGradientBrush StartPoint="0,0" EndPoint="0,1"><LinearGradientBrush.GradientStops><GradientStop Color="#FFFAFBE9" Offset="0" /><GradientStop Color="#FFFFA500" Offset="1" /></LinearGradientBrush.GradientStops></LinearGradientBrush></Setter.Value></Setter><Setter Property="Shape.Stroke"><Setter.Value><SolidColorBrush>#FFD69436</SolidColorBrush></Setter.Value></Setter><Setter Property="Shape.StrokeThickness"><Setter.Value><s:Double>1</s:Double></Setter.Value></Setter><Setter Property="Shape.StrokeLineJoin"><Setter.Value><x:Static Member="PenLineJoin.Round" /></Setter.Value></Setter><Setter Property="Shape.Stretch"><Setter.Value><x:Static Member="Stretch.Fill" /></Setter.Value></Setter><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>False</s:Boolean></Setter.Value></Setter><Setter Property="UIElement.SnapsToDevicePixels"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="Shape.StrokeDashArray"><Setter.Value><DoubleCollection>1.5 0.7</DoubleCollection></Setter.Value></Setter></Style></Style.BasedOn><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property="UIElement.IsHitTestVisible"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter><Setter Property="Shape.Fill"><Setter.Value><SolidColorBrush>#00FFFFFF</SolidColorBrush></Setter.Value></Setter><Setter Property="Shape.Stroke"><Setter.Value><SolidColorBrush>#00FFFFFF</SolidColorBrush></Setter.Value></Setter></Style></Rectangle.Style></Rectangle></ControlTemplate></dd:DesignerItem.DragThumbTemplate></Rectangle></Content>
</ConfigurationItem>
</ConfigurationItems>
<Connections />
</Topologies>
答案 0 :(得分:1)
如果您只想解析xml节点中的文本(由于您没有提供有关xml结构的任何信息,您必须自己获取该文本),您可以采用令人讨厌的方式:
string yourNodeValue = @"Shape=""Rectangle"" Tooltip=""data"" StrokeThickness=""2"" Tag=""default""";
string output;
var keysAndValues = yourNodeValue.Split(' ').Select (nv => nv.Trim())
foreach(var keyValue in keysAndValues)
{
var pair = keyValue.Split('=');
if(pair[0] == "Tooltip")
{
output = pair[1].Trim('"');
break;
}
}
但是,我假设将始终保留key="value"
格式,之间没有空格。
或者你可以使用正则表达式灌浆模式Shape=\"(\w+)\"
答案 1 :(得分:1)
您可以尝试这样的事情:
var xml = "<Content> Shape=\"Rectangle\" Tooltip=\"data\" StrokeThickness=\"2\" Tag=\"default\" </Con" +
"tent>";
using(var xmlReader = XmlReader.Create(new StringReader(xml)))
{
while(xmlReader.Read())
{
var regex = "Tooltip=\"[a-zA-Z0-9]+\"";
var match = Regex.Match(xmlReader.Value, regex).Value.Replace("Tooltip=\"", "").Replace("\"", "");
if(!string.IsNullOrEmpty(match))
{
var restuledString = match;
}
}
}
答案 2 :(得分:0)
如果您深入了解文件,您可能会发现它是Content-tag中的xml结构。 你可以这样读它然后加载为xml内容:
var xmlfilecontent = IO.File.ReadAllText("yourfilename");
xmlfilecontent = xmlfilecontent.Replace("><", ">\n<");
xmlfilecontent = xmlfilecontent.Replace("<", "<").Replace(">", ">");
IO.File.WriteAllText("yourfilenamel", xmlfilecontent);