【问题标题】:Get values from an xml (4 levels)从 xml 获取值(4 个级别)
【发布时间】:2018-11-06 23:52:45
【问题描述】:

一个xml中各个级别的值的帮助。

这是xml:

<widgets>
    <id>95</id>

    <widget type="1" name="accventas" caption="Ofertas ventas" flags="4">
        <service name="pm_venofer_total00001" caption="Pendientes de aceptar" desc="" type="3" detail="1">
            <xvalue>20</xvalue>
            <xcolor>1</xcolor>
        </service>
    </widget>

    <widget type="3" name="today_state" caption="Estado de ventas" flags="4">
        <service name="pd_today_orders00001" caption="Pedidos" desc="Nº pedidos del día" type="3" detail="1">
            <xvalue>0</xvalue>
            <xcolor>2</xcolor>
            <xalert>No se está vendiendo nada</xalert>
        </service>

        <service name="pd_today_sales00001" caption="Importe" desc="Importe ventas del día" type="3" detail="1">
            <xvalue>0,00</xvalue>
            <xcolor>2</xcolor>
            <xalert>No estamos recaudando nada</xalert>
        </service>
    </widget>
</widgets>

已加载xml并准备试用,但我无法获取您需要的所有字段

我需要:

  • 身份证,
  • 小部件的标题属性,
  • 每个小部件的服务,
  • 服务的标题属性,
  • x 值,
  • xcolor 和 xalert,
  • 每项服务

我得到所有的小部件,像这样:(我认为有两种:EmployeesEmployee

[XmlRoot("widgets")]
public class Employees
{
    [XmlElement("widget")]
    public ObservableCollection <Employee> Coleccion { get; set; }
}


 public class Employee
 {
    [XmlAttribute("caption")]
    public string nombreWidget { get; set; }
 }

但不像进入每个小部件各自的服务(服务属性),以及在这些 xValue、xcolor 和 xalert 中

【问题讨论】:

  • 您是否考虑过使用 Linq to XML 或 XPATH?还是必须使用XmlSerializer
  • 我正在使用 LINQ to XML,因为不幸的是 XPath 目前不支持,并且 linq 无法访问所有标签

标签: c# xml windows-phone-7 xml-serialization


【解决方案1】:

LinqToXml 解决方案:

var xml = XDocument.Parse(Resource1.XMLFile1).Root;
var parsed = new {
                     Id = xml.Element("id").Value,
                     Widgets = xml.Elements("widget")
                                  .Select(w => new
                                  {
                                      Caption = w.Attribute("caption").Value,
                                      Services = w.Elements("service").Select(s => new
                                      {
                                          Caption = s.Attribute("caption").Value,
                                          XColor = s.Element("xcolor").Value,
                                          XValue = s.Element("xvalue").Value,
                                          XAlert = s.Element("xalert") != null ? s.Element("xalert").Value : null
                                      }).ToList()
                                  }).ToList()
                 };

它将创建代表您的输入 XML 的匿名对象。您可以轻松地将我的代码中的匿名对象替换为您的真实域对象(Employees 等)。

【讨论】:

    【解决方案2】:

    你应该使用 XPATH

    using System.Xml.XPath;
    

    然后做这样的事情:

    XPathNavigator nav;
    XPathDocument docNav;
    XPathNodeIterator NodeIter;
    String strExpression;
    
    // Open the XML.
    docNav = new XPathDocument(@"c:\books.xml");
    
    // Create a navigator to query with XPath.
    nav = docNav.CreateNavigator();
    
    // Find the average cost of a book.
    // This expression uses standard XPath syntax.
    strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)";
    

    有关使用 XPATH 可以实现的所有功能的更多信息,请考虑: https://developer.mozilla.org/en/docs/XPath

    【讨论】:

    • 我正在开发一个 Window Phone 应用程序,我需要将 xpath 与 linq 一起使用。不幸的是,目前不支持 xpath。
    【解决方案3】:

    95

    <widget type="1" name="accventas" caption="Ofertas ventas" flags="4">
        <service name="pm_venofer_total00001" caption="Pendientes de aceptar" desc="" type="3" detail="1">
            <xvalue>20</xvalue>
            <xcolor>1</xcolor>
        </service>
    </widget>
    
    <widget type="3" name="today_state" caption="Estado de ventas" flags="4">
        <service name="pd_today_orders00001" caption="Pedidos" desc="Nº pedidos del día" type="3" detail="1">
            <xvalue>0</xvalue>
            <xcolor>2</xcolor>
            <xalert>No se está vendiendo nada</xalert>
        </service>
    
        <service name="pd_today_sales00001" caption="Importe" desc="Importe ventas del día" type="3" detail="1">
            <xvalue>0,00</xvalue>
            <xcolor>2</xcolor>
            <xalert>No estamos recaudando nada</xalert>
        </service>
    </widget>
    

    【讨论】:

    • 你需要解释你的代码,为什么这是问题的答案?
    • 嗨,马特,欢迎来到 Stack Overflow ?。虽然这可能会回答作者的问题,但它缺少一些解释性文字和/或文档链接。如果没有围绕它们的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现how to write a good answer 非常有帮助。请编辑您的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多