如何在XML中搜索所有元素
本文关键字:元素 搜索 XML | 更新日期: 2023-09-27 18:25:58
这是我的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]
您可能可以为您的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;
}
}
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。