3

帮助在不同级别的 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 并准备好试用,但我无法获得您需要的所有字段

我需要:

  • ID,
  • 小部件的标题属性,
  • 每个小部件的服务,
  • 服务的标题属性,
  • 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

4

3 回答 3

0

你应该使用 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

于 2012-12-17T10:21:14.267 回答
0

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等)。

于 2012-12-21T20:10:20.103 回答
0

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>

于 2018-11-06T15:52:45.467 回答