我想搜索xml文件中的元素值並從中獲取特定的字符串。舉例來說,我有以下元素值在我的XML文件:c#:在字符串數組中搜索並獲取特定的字符串值
<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>
您錯誤鍵入了XML嗎? ''看起來像屬性應該在''標籤內 –
發佈完整的XML或完整的樣本,我們應該如何建議只有一個節點? – DGibbs
MSDN上有很多關於解析xml的教程.. – Sayse