从第一个xml子节点中选择值

时间:2015-07-16 03:23:55

标签: c# xml

我有一个XML文件,其中包含针对不同图像文件大小的多个网址,我试图将单个网址加载到图片框中。我的问题是子节点的命名方式相似,父节点的命名也相似。例如,我想拉第一个中间图像(以SL160_.jpg结尾)。请参阅下面的XML代码

<Items>
<SmallImage>
    <URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL._SL75_.jpg</URL>
</SmallImage>
<MediumImage>
    <URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL._SL160_.jpg</URL>
</MediumImage>
<LargeImage>
    <URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL.jpg</URL> 
</LargeImage>
<MediumImage>
    <URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL._SL162_.jpg</URL> 
</MediumImage>
<LargeImage>
    <URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL.jpg</URL> 
</LargeImage>
</Items>

我已尝试使用GetElementsByTag,并试图调用doc.SelectSingleNode("LargeImage").SelectSingleNode("URL").InnerTextGetElementByID之类的内容。所有这些都给了我一个Object set to null reference例外。

如何指定我想要从第一个找到的MediumImage节点获取网址?

3 个答案:

答案 0 :(得分:0)

使用LinqToXML,这很简单

string xml = @"<Items>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL._SL75_.jpg</URL>
</SmallImage>
<MediumImage>
<URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL.01_SL160_.jpg</URL>
</MediumImage>
<LargeImage>
<URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL.jpg</URL> 
</LargeImage>
<MediumImage>
<URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL.02_SL162_.jpg</URL> 
</MediumImage>
<LargeImage>
<URL>http://ecx.images-amazon.com/images/I/51TAL%2Bn7AqL.jpg</URL> 
</LargeImage>
</Items>";
XElement root = XElement.Parse(xml);
var ele = root.Elements("MediumImage").Where(e => e.Element("URL").Value.EndsWith("SL160_.jpg")).FirstOrDefault();
Console.WriteLine(ele);

答案 1 :(得分:0)

除了Sky Fang的回答,我认为OP想要这个:

var firstMedImg = root.Elements("MediumImage").First();
var imgUrl = firstMedImg.Element("URL").Value;

答案 2 :(得分:0)

        XmlDocument doc = new XmlDocument();

        // PATH TO YOUR DOCUMENT
        doc.Load("daco.xml");

        // Select LIST ALL ELEMENTS SmallImage,MediumImage,LargeImage
        XmlNodeList listOfAllImageElements = doc.SelectNodes("/Items/*");

        foreach (XmlNode imageElement in listOfAllImageElements)
        {
            // Select URL ELEMENT
            XmlNode urlElement= node.SelectSingleNode("URL");
            System.Console.WriteLine(urlElement.InnerText);
        }

        Console.ReadLine();

如果您想选择多个网址

        XmlDocument doc = new XmlDocument();

        // PATH TO YOUR DOCUMENT
        doc.Load("daco.xml");

        // Select LIST ALL ELEMENTS SmallImage,MediumImage,LargeImage
        XmlNodeList listOfAllImageElements = doc.SelectNodes("/Items/*");

        foreach (XmlNode imageElement in listOfAllImageElements)
        {
            // Select URL's ELEMENTs
            XmlNodeList listOfAllUrlElements = imageElement.SelectNodes("URL");
            foreach (XmlNode urlElement in listOfAllUrlElements)
            {
                System.Console.WriteLine(urlElement.InnerText);
            }
        }

        Console.ReadLine();

如果您的xml文件中有特定的命名空间

        XmlDocument doc = new XmlDocument();
        doc.Load("doc.xml");

        XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
        // reaplace http://schemas.microsoft.com/vs/2009/dgml with your namespace
        man.AddNamespace("x", "http://schemas.microsoft.com/vs/2009/dgml");

        // next you have to use x: in your path like this 
        XmlNodeList node = doc.SelectNodes("/x:Items/x:*, man);