简体   繁体   中英

Extracting a Value From an XML File in C#

Can someone help me out with extracting the "Value Y" of 4.247 in this XML file. I've searched through previous threads but can't seem to apply them to this structure.

<Chart1>
    <Chart1_SeriesGroup_Collection>
        <Chart1_SeriesGroup Label="MP_Trend_Data\MP_Prog/MP_DAILY_FLOW">
            <Chart1_CategoryGroup_Collection>
                <Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
                   <Value X="0001-01-01T10:30:00+10:30"/>
               </Chart1_CategoryGroup>
               <Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
                   <Value X="0001-01-01T10:30:00+10:30"/>
               </Chart1_CategoryGroup>
               <Chart1_CategoryGroup Label="12/24/2017 1:59:19 AM">
                   <Value X="0001-01-01T10:30:00+10:30"/>
               </Chart1_CategoryGroup>
               <Chart1_CategoryGroup Label="12/24/2017 1:59:19 AM">
                   <Value X="0001-01-01T10:30:00+10:30"/>
               </Chart1_CategoryGroup>
               <Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
                   <Value Y="4.24700021743774" X="2017-12-24T12:28:19.333+10:30"/>
               </Chart1_CategoryGroup>

code I've tried:

var xDoc = XDocument.Load("C:\\attachment\\Flow.xml");

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(xDoc.ToString());

        XmlNodeList xnList = xml.SelectNodes("/Chart1/Chart1_SeriesGroup_Collection/Chart1_SeriesGroup[@Label='MP_Trend_Data\\MP_Prog/MP_DAILY_FLOW']");

        foreach (XmlNode Node in xnList)
        {
            XmlNodeList ynList = Node.SelectNodes("/Chart1_CategoryGroup_Collection/Chart1_CategoryGroup/Value");
            foreach (XmlNode Node2 in ynList)
            {
                textBox1.Text = Node2.Attributes["Y"].Value.ToString();
            }
        }
var xmlStr = @"
    <Chart1>
        <Chart1_SeriesGroup_Collection>
            <Chart1_SeriesGroup Label='MP_Trend_Data\MP_Prog/MP_DAILY_FLOW'>
                <Chart1_CategoryGroup_Collection>
                    <Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
                        <Value X='0001-01-01T10:30:00+10:30'/>
                    </Chart1_CategoryGroup>
                    <Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
                        <Value X='0001-01-01T10:30:00+10:30'/>
                    </Chart1_CategoryGroup>
                    <Chart1_CategoryGroup Label='12/24/2017 1:59:19 AM'>
                        <Value X='0001-01-01T10:30:00+10:30'/>
                    </Chart1_CategoryGroup>
                    <Chart1_CategoryGroup Label='12/24/2017 1:59:19 AM'>
                        <Value X='0001-01-01T10:30:00+10:30'/>
                    </Chart1_CategoryGroup>
                    <Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
                        <Value Y='4.24700021743774' X='2017-12-24T12:28:19.333+10:30'/>
                    </Chart1_CategoryGroup>
                </Chart1_CategoryGroup_Collection>>
           </Chart1_SeriesGroup>
        </Chart1_SeriesGroup_Collection>
    </Chart1>";

var xml = XElement.Parse(xmlStr);
var firstY = xml.Descendants("Value").FirstOrDefault(v => v.Attribute("Y") != null);
if (firstY != null)
{
    var y = firstY.Attribute("Y").Value;
}

Try xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("Chart1_CategoryGroup").Where(x => x.Element("Value").Attribute("Y") != null).Select(x => new {
                label = (DateTime)x.Attribute("Label"),
                y = (double)x.Element("Value").Attribute("Y"),
                x = (DateTime)x.Element("Value").Attribute("X")
            }).ToList();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM