根据标签选择XmlNode的子节点

时间:2018-10-13 08:41:22

标签: c# xml xpath

我正在尝试解析此XML文档:

<MPD>
  <Period duration="PT0H3M1.63S" start="PT0S">
    <AdaptationSet>
      <ContentComponent contentType="video" id="1" />
      <Representation bandwidth="4190760" codecs="avc1.640028" height="1080" id="1" mimeType="video/mp4" width="1920">
        <BaseURL>car-20120827-89.mp4</BaseURL>
      </Representation>
      <Representation bandwidth="2073921" codecs="avc1.4d401f" height="720" id="2" mimeType="video/mp4" width="1280">
        <BaseURL>car-20120827-88.mp4</BaseURL>
      </Representation>
    </AdaptationSet>
    <AdaptationSet>
      <ContentComponent contentType="audio" id="2" />
      <Representation bandwidth="127236" codecs="mp4a.40.2" id="6" mimeType="audio/mp4" numChannels="2" sampleRate="44100">
        <BaseURL>car-20120827-8c.mp4</BaseURL>
      </Representation>
      <Representation bandwidth="255236" codecs="mp4a.40.2" id="7" mimeType="audio/mp4" numChannels="2" sampleRate="44100">
        <BaseURL>car-20120827-8d.mp4</BaseURL>
      </Representation>
    </AdaptationSet>
  </Period>
</MPD>

使用此C#代码:

var rootDoc = new XmlDocument();
rootDoc.LoadXml(xmlString); // the one from above

var adaptationSets = rootDoc.GetElementsByTagName("AdaptationSet");

if (adaptationSets.Count > 0)
    foreach (XmlNode adaptionSet in adaptationSets) // Loop through AdaptionSets
    {
        // Get the one Node in this AdaptionSet with the ContentComponent-Tag
        var contentComponent = adaptionSet.SelectSingleNode("ContentComponent");

        if (contentComponent != null)
        {
            // parse attributes
        }

        // Get All Nodes in this AdaptionSet with the Representation-Tag
        var representations = adaptionSet.SelectNodes("Representation");

        if(representations?.Count > 0)
            foreach (XmlNode representation in representations)
            {
                // parse attributes of XmlNode
            }
    }

除XPath查询外,所有其他功能均有效。我尝试了很多以“ /”、“//”、“./”开头的变体,并且没有任何前导字符,但它根本无法正常工作。我究竟做错了什么?我没有定期使用XPath,除了提到的主要字符外,我找不到其他任何东西。因为我在该网站上的许多其他答案中都看到了它,所以我想我应该提一提我明确地在寻找可以帮助我解决此问题的XPath,而不是某些Linq变体或完全不同的方法。

任何帮助将不胜感激。预先感谢!

2 个答案:

答案 0 :(得分:1)

我认为这可以解决您的问题:

1.1 --> 1
1.4 --> 2
1.5 --> 3
1.9 --> 4
2.0 --> 5
2.1 --> 6

答案 1 :(得分:0)

尝试使用xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement period = doc.Descendants("Period").FirstOrDefault();

            MPD mpd = new MPD();
            mpd.duration = (string)period.Attribute("duration");
            mpd.start = (string)period.Attribute("start");
            mpd.adaptions = period.Elements("AdaptationSet").Select(x => new Adaption() {
                contentType = (string)x.Element("ContentComponent").Attribute("contentType"),
                id = (int)x.Element("ContentComponent").Attribute("id"),
                representations = x.Elements("Representation").Select(y => new Representation() {
                    bandwidth = (int)y.Attribute("bandwidth"),
                    codecs = (string)y.Attribute("codecs"),
                    height = (int?)y.Attribute("height"),
                    id = (int)y.Attribute("id"),
                    mimeType = (string)y.Attribute("mimeType"),
                    width = (int?)y.Attribute("width"),
                    baseURL = (string)y.Descendants("BaseURL").FirstOrDefault()
                }).ToList()
            }).ToList();

        }
    }
    public class MPD
    {
        public string duration { get; set; }
        public string start { get; set; }
        public List<Adaption> adaptions { get; set; }
    }
    public class Adaption
    {
        public string contentType { get; set; }
        public int id { get; set; }
        public List<Representation> representations { get; set; }
    }
    public class Representation
    {
        public int bandwidth { get; set; }
        public string codecs { get; set; }
        public int? height { get; set; }
        public int id { get; set; }
        public string mimeType { get; set; }
        public int? width { get; set; }
        public string baseURL { get; set; }
    }

}