如何在XML中搜索所有元素

3
这是我的XML源。它还有更多内容。
 <Cproducts>
   <ID>001</ID>
   <Name>name one</Name>
   <Availability>
        <Departure>
          <Date>2015-12-03T00:00:00.0000000+00:00</Date>
          <Pricing>
            <Price>
              <Type>ADT</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
            <Price>
              <Type>CHD</Type>
              <Value>95.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>INF</Type>
              <Value>0.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>FAM</Type>
              <Value>0.00</Value>
              <Qty>0</Qty>
            </Price>
            <Price>
              <Type>SEN</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
          </Pricing>
        </Departure>
        <Departure>
          <Date>2015-12-06T00:00:00.0000000+00:00</Date>
          <Pricing>
            <Price>
              <Type>ADT</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
            <Price>
              <Type>CHD</Type>
              <Value>95.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>INF</Type>
              <Value>0.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>FAM</Type>
              <Value>0.00</Value>
              <Qty>0</Qty>
            </Price>
            <Price>
              <Type>SEN</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
          </Pricing>
        </Departure>
    <Availability>
 </Cproducts>  

在我的情况下,我希望能够遍历所有可用的日期。也就是说,当用户从表单中选择一个日期时,根据该日期提供其他数据。为了实现这一点,我必须查看所有可用的日期。我应该如何做呢?

以下是我在MVC中的代码:

public void getDatas(string destination, string cruisetype, string datetime)
{
    XElement rootele = XElement.Load(Server.MapPath("~/XmlFiles/CruiseData/cruiseprodutstwo.xml"));

    var selecttoDate = rootele.Elements("Cproducts").Elements("Availability").Elements("Departure").Elements("Date"); //here I want to get the data that the [date == datetime]

我真的不明白为什么这个问题会得到三个赞 :-( - Uwe Keim
请问是否有直接的方法可以查看特定日期是否可用。例如下面的示例:var getneededData = rootele.Elements("CruiseProduct") .Where(l => l.Element("Location").Value == destination );使用此方法,我们可以在整个文档中检查并获取 Location 等于给定字符串的数据。我想要通过整个日期进行搜索。@Uwe Keim 先生,能否帮助我解决这个问题。 - user5541842
2个回答

1
    static void Main(string[] args)
    {
        string xml = @"<Cproducts>   
<Availability>
        <Departure>
          <Date>2015-12-03T00:00:00.0000000+00:00</Date>
          <Pricing>
            <Price>
              <Type>ADT</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
            <Price>
              <Type>CHD</Type>
              <Value>95.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>INF</Type>
              <Value>0.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>FAM</Type>
              <Value>0.00</Value>
              <Qty>0</Qty>
            </Price>
            <Price>
              <Type>SEN</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
          </Pricing>
        </Departure>
        <Departure>
          <Date>2015-12-06T00:00:00.0000000+00:00</Date>
          <Pricing>
            <Price>
              <Type>ADT</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
            <Price>
              <Type>CHD</Type>
              <Value>95.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>INF</Type>
              <Value>0.00</Value>
              <Qty>5</Qty>
            </Price>
            <Price>
              <Type>FAM</Type>
              <Value>0.00</Value>
              <Qty>0</Qty>
            </Price>
            <Price>
              <Type>SEN</Type>
              <Value>175.00</Value>
              <Qty>20</Qty>
            </Price>
          </Pricing>
        </Departure>
    </Availability>
 </Cproducts>";

        XDocument doc = XDocument.Parse(xml);

        var list = (from element in doc.Elements("Cproducts").Elements("Availability").Elements("Departure")
                    where Convert.ToDateTime(element.Element("Date").Value) < DateTime.Now
                    select element).ToList();

        foreach(XElement el in list)
        {
            Console.WriteLine(el.Element("Date").Value);
        }

    }

我不明白你想要做什么。因此,我为您编写了一种从XML中选择日期的方法,其中日期小于今天。在您的情况下,您不应使用XDocument.Parse,而是需要使用XDocument.Load(uri)。另外下次请至少尝试添加有效的XML。

1
你可以通过以下方式为你的XML创建序列化:
[XmlRoot(ElementName="Price")]
public class Price {
    [XmlElement(ElementName="Type")]
    public string Type { get; set; }
    [XmlElement(ElementName="Value")]
    public string Value { get; set; }
    [XmlElement(ElementName="Qty")]
    public string Qty { get; set; }
}

[XmlRoot(ElementName="Pricing")]
public class Pricing {
    [XmlElement(ElementName="Price")]
    public List<Price> Price { get; set; }
}

[XmlRoot(ElementName="Departure")]
public class Departure {
    [XmlElement(ElementName="Date")]
    public string Date { get; set; }
    [XmlElement(ElementName="Pricing")]
    public Pricing Pricing { get; set; }
}

[XmlRoot(ElementName="Availability")]
public class Availability {
    [XmlElement(ElementName="Departure")]
    public List<Departure> Departure { get; set; }
}

[XmlRoot(ElementName="Cproducts")]
public class Cproducts {
    [XmlElement(ElementName="Availability")]
    public Availability Availability { get; set; }
}

如果您获取XML中对象的ObservableCollection,那么查询任何复杂数据将更加容易。因此,在当前情况下,您可以在序列化后创建Cproducts的ObservableCollection,然后使用LINQ进行查询。

ObservableCollection<Cproducts> ResultantCollection = new ObservableCollection<Cproducts>();
if(File.Exists(path + "\\YourXML.xml"))
{
    XElement root = XElement.Load(path + "\\YourXML.xml");
    root.Element("Cproducts").Elements("Availability").All<XElement>(xe =>
    {
        ResultantCollection.AddAvailability(Availability av);
        return true;
    });

这个类将会被定义成这样

public class CproductsColl : ObservableCollection<Cproducts>
{
    public User AddAvailability(Availability av)
    {
        base.Add(av);
        return av;
    }
}       

如果可以的话,您能否向我展示如何创建Cproducts的ObservableCollection,因为我是这个领域的新手。 - user5541842
我不知道该怎么继续下去,是应该添加到列表中还是怎样。请@Mohit帮帮我。 - user5541842
`XElement cElement = XElement.Load(Server.MapPath("~/XmlFiles/CruiseData/cruiseprodutstwo.xml")); var getdataa = cElement.Elements("CruiseProduct"); ObservableCollection ResultantCollection = new ObservableCollection();` - user5541842
@anu:你应该仔细阅读ObservableCollection教程YouTube:C#中的Observable Collection演示,如果需要帮助,请告诉我。 - Mohit S
@anu:写出答案并不是一个简单的概念,但提供的链接将有更多的链接,这肯定会让您了解如何实现ObservableCollections,并使您的工作更快、更容易。 - Mohit S
谢谢您的评论,但我想知道如何将返回的XML添加到ResultantCollection中。 - user5541842

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接