[英]Extracting data from the properties of an xml file
I am attempting to extract data from an xml file generated from a save function. 我正在尝试从保存功能生成的xml文件中提取数据。 Here is what the xml looks like when the data has been serialized
这是数据序列化后xml的样子
<Data>
<ParentID>00000000-0000-0000-0000-000000000000</ParentID>
<Content><ContentControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Grid><Image Source=".//Resources/Images/start.png" Tag="Start" ToolTip="Start" IsHitTestVisible="False" /></Grid></ContentControl> </Content>
</Data>
I can read the data between the <> signs using an XElement object and extract it value using Element("Child").Value for example the ParentID but I do not know how to extract the property data from within Content tags such as the programmatic reading the Tag property of the Image, in this case Tag='Start'. 我可以使用XElement对象读取<>符号之间的数据,并使用Element(“ Child”)。Value提取其值。例如ParentID,但我不知道如何从Content标记(例如程序化)中提取属性数据读取图片的Tag属性,在这种情况下为Tag ='Start'。
Can someone please assist me to resolve this matter 有人可以帮我解决这个问题吗
If the problem you are running into is that the data in the Content node is a malformed fragment, then this is a way to extract that, fix the malformation and get at the data. 如果您遇到的问题是“内容”节点中的数据是格式错误的片段,那么这是一种提取方法,修复格式错误并获取数据的方法。
string asReadXml = @"<Data>
<ParentID>00000000-0000-0000-0000-000000000000</ParentID>
<Content><ContentControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <Grid><Image Source="".//Resources/Images/start.png"" Tag=""Start"" ToolTip=""Start"" IsHitTestVisible=""False"" /></Grid></ContentControl> </Content>
</Data>";
var fragment = Regex.Match(asReadXml, @"(?:\<Content\>)(?<Xml>.+)(?:\</Content\>)", RegexOptions.ExplicitCapture).Groups["Xml"].Value;
var validFragment = Regex.Replace(Regex.Replace(fragment, "(<)", "<"), "(>)", ">");
var xDoc = XDocument.Parse("<Root>" + validFragment + "</Root>");
/* XDoc looks like this:
<Root>
<ContentControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid>
<Image Source=".//Resources/Images/start.png" Tag="Start" ToolTip="Start" IsHitTestVisible="False" />
</Grid>
</ContentControl>
</Root>
*/
var Image =
xDoc.Root
.Descendants()
.Where (p => p.Name.LocalName == "Image")
.First ();
Console.WriteLine ( Image.Attribute("Tag").Value );
// Outputs
// Start
var data = @"<Data>" +
"<ParentID>00000000-0000-0000-0000-000000000000</ParentID>" +
"<Content><ContentControl xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"+
"<Grid><Image Source=\".//Resources/Images/start.png\" Tag=\"Start\" ToolTip=\"Start\" IsHitTestVisible=\"False\" /></Grid></ContentControl>" +
"</Content>" +
"</Data>";
var root = XElement.Parse(data);
var contentValue = root.Element("Content").Value;
var contentXml = XElement.Parse(contentValue);
var ns = contentXml.Name.Namespace; // retrieve the namespace
var imageTagValue = contentXml.Element(ns+"Grid").Element(ns+"Image").Attribute("Tag").Value; //
Assume that element
is an XElement object that represent <Content>
element (You already have a way to get it though), you can do as follow to get Tag
attribute value of Image
element : 假设
element
是一个表示<Content>
元素的XElement对象(尽管您已经有一种获取方法),可以按照以下步骤获取Image
元素的Tag
属性值:
XElement element = ....;
var content = XElement.Parse((string)element);
var ns = content.Name.Namespace;
var image = content.Descendants(ns + "Image").FirstOrDefault();
var tag = "";
if(image != null)
{
tag = (string)image.Attribute("Tag");
}
We check if image
is null before looking for it's attribute. 我们先检查
image
是否为空,然后再查找其属性。 With that, you won't get exception if there any <Content>
element that doesn't have <Image>
element). 这样,如果有任何
<Content>
元素没有<Image>
元素,您将不会获得异常。 tag
variable will simply contains empty string in that case. 在这种情况下,
tag
变量将仅包含空字符串。
This also handle case when <Content>
has <Image>
element resides in different path (not under <Grid>
element). 当
<Content>
具有<Image>
元素位于不同路径(不在<Grid>
元素下)时,也可以处理这种情况。
Personally, I would recommend getting the whole content as a string, and then parse it as a html data using http://htmlagilitypack.codeplex.com/ library. 就个人而言,我建议以字符串形式获取全部内容,然后使用http://htmlagilitypack.codeplex.com/库将其解析为html数据。 That way you'll offload all the parsing to specialized libraries.
这样,您就可以将所有解析工作转移到专用库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.