如何搜索XML中的所有元素

用户名

这是我的XML feed。它不止如此。

 <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]
莫希特·什里瓦斯塔瓦(Mohit Shrivastava)

您可能可以像这样为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;
    }
}       

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何恢复XML标记中的所有XML元素?

如何在不加载所有元素的情况下搜索特定元素的 XML 文件?

在所有框架中搜索元素

如何从XML导入Google表格中的所有元素节点

如何在xml中搜索元素?

如何在具有字典表示的所有元素的数据框中搜索一行?

如何通过 XSLT 计算 XML 中我的元素上方的所有元素?

如何点击所有产品链接,搜索元素并返回。

如何使用angular2 primeng使子元素在搜索中显示并在单击All时显示所有元素

python elementtree - 如何在xml中查找具有特定属性的所有元素

在XML中添加新元素时所有元素重复

如何使用CURL和DOM将xml文件中的所有元素放入数组中?

如何测试列表中的所有元素

如何从元素中删除所有属性

如何打印集合中的所有元素?

如何移动表格中的所有元素?

如何从Java中的XML字符串获取所有元素的值?

如何在选择节点中遍历XML中的所有元素

如何从 Oracle 表 clob 列中查找 xml 文件中所有元素和属性的列表?

如何将XML中所有元素的值放入数组中?

如何从一个 XML 元素中删除所有属性?

如何使用c#从XML文件中检索所有元素

如何使用带有芭蕾舞演员的Xpath在XML有效负载中搜索元素?

如何在弹性搜索中指定的所有字段中搜索所有字段?

如何选择XML中的所有列

如何使用Go获取XML元素的所有属性?

如果重复,如何删除xml标签的所有元素[python]

如何通过XQuery从XML获取所有元素

如何使用xpath查找特定类型的所有xml元素?