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、
  • 各サービス

次のように、すべてのウィジェットを取得します: (2 種類あると思います: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

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

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 に答える