c# - 如何搜索 XML 中的所有元素

标签 c# xml

这是我的 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;
    }
}       

关于c# - 如何搜索 XML 中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126422/

相关文章:

c# - 如何在 c# wpf 中使用本地数据库 .sdf?

c# - 更改 LineRenderer 绘制顺序

java - Xquery 正在返回一组元素的结果而不是其他元素?

c# - .Net 中(对称)加密的最佳实践?

c# - 为什么我无法将名称值分配给 WPF 中范围为“内部”的 UserControl?

java - 3 个 TextView 旁边的 Android 布局按钮

xml - 在纯 ABAP 中针对 XSD 验证 XML

php - 如何使用 php 将 .xlsx 文件转换为 .xml 文件?

java - 在 JAVA 中使用 XPath 解析具有不同命名空间的 xml

c# - 检查 8 位字符是否为字母数字