C#,XML解析。获取标签之间的数据 [英] C# , xml parsing. get data between tags

查看:445
本文介绍了C#,XML解析。获取标签之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:< XML版本=

  responsestring = 1.0编码=utf-8 > 
<上传><图像><名称>< /名称><散> SOmetext< /散列>中



我如何获得



<之间的值pre> <&哈希GT;和< /散列> ?





我尝试:

  responseString.Substring(responseString.LastIndexOf(<&哈希GT;)+ 6,8); //这种作品的,但不会在任何情况下工作。 



也试图与XMLReader可以乱搞,也没有找到解决办法。



TY


解决方案

其他建议的LINQ to XML解决方案,这就是我想要的使用还有,如果可能的话。



如果你坚持使用.NET 2.0,使用的XmlDocument 甚至的XmlReader



不过的的尝试使用操纵自己的原始字符串子串的IndexOf 。使用的部分的说明的XML API。否则,你的搞错了。它使用了合适的工具的问题。解析XML正确的工作显著块 - 包括已经完成的工作。



现在,只是为了让这一个完整的答案,这里是使用您的样品简短而完整的程序数据:

 使用系统; 
使用System.Xml.Linq的;

类测试
{
静态无效的主要()
{
串响应= @< XML版本='1.0'编码=' UTF-8'>
<上传><图像><名称>< /名称><散>有的文字< /散列>< /图片>< /上传>中;

的XDocument DOC = XDocument.Parse(响应);

的foreach(的XElement hashElement在doc.Descendants(哈希))
{
串散列值=(字符串)hashElement;
Console.WriteLine(散列值);
}
}
}



显然,这都将环比所有的的元素。如果你只想要一个,你可以使用 doc.Descendants(哈希)。单() doc.Descendants(哈希)。首先()根据您的要求。



请注意,无论我在这里使用和转换属性将返回的所有的文本元素中节点的串联。但愿没关系你 - 或者你可以得到的只是这是一个直接的孩子,如果有必要的第一个文本节点


I have a string :

responsestring = "<?xml version="1.0" encoding="utf-8"?>
<upload><image><name></name><hash>SOmetext</hash>"

How can i get the value between

<hash> and </hash>

?

My attempts :

responseString.Substring(responseString.LastIndexOf("<hash>") + 6, 8); // this sort of works , but won't work in every situation.

also tried messing around with xmlreader , but couldn't find the solution.

ty

解决方案

Others have suggested LINQ to XML solutions, which is what I'd use as well, if possible.

If you're stuck with .NET 2.0, use XmlDocument or even XmlReader.

But don't try to manipulate the raw string yourself using Substring and IndexOf. Use an XML API of some description. Otherwise you will get it wrong. It's a matter of using the right tool for the job. Parsing XML properly is a significant chunk of work - work that's already been done.

Now, just to make this a full answer, here's a short but complete program using your sample data:

using System;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        string response = @"<?xml version='1.0' encoding='utf-8'?>
<upload><image><name></name><hash>Some text</hash></image></upload>";

        XDocument doc = XDocument.Parse(response);

        foreach (XElement hashElement in doc.Descendants("hash"))
        {
            string hashValue = (string) hashElement;
            Console.WriteLine(hashValue);
        }
    }
}

Obviously that will loop over all the hash elements. If you only want one, you could use doc.Descendants("hash").Single() or doc.Descendants("hash").First() depending on your requirements.

Note that both the conversion I've used here and the Value property will return the concatenation of all text nodes within the element. Hopefully that's okay for you - or you could get just the first text node which is a direct child if necessary.

这篇关于C#,XML解析。获取标签之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆