LINQ2XML方法通过xml推广迭代

时间:2016-04-01 10:49:31

标签: c# xml linq

我正在尝试解析一些xml文件(使用LINQ)以使用该数据存储在DB中。以下是其中一个服务的格式,其中有几个更多,每个都有不同的节点模式和结构。

有没有办法概括这种方法,以便我可以通过单个方法/代码行迭代所有这些服务?没有被childNodes的安排困扰?

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<deviceState>
<id>203948-2908093-203984902-29348020-290lk</id>
<device>Mirron</device>
<variable>
      <id>Plug - Meter.VI</id>
      <text>
         <textValue>
            <value>0.000000</value>
         </textValue>
      </text>
   </variable>

 <variable>
      <id>AEB</id>
      <text>
         <textStr>-</textStr>
      </text>
   </variable>
 <variable>
      <id>DESCRIPTION</id>
      <text>
         <textStr />
      </text>
   </variable>
 <variable>
      <id>VDTTM</id>
      <text>
         <textDate>
            <date>01042016103658</date>
         </textDate>
      </text>
   </variable>
 <variable>
      <id>STATUS</id>
      <text>
         <textValue>
            <value>1.000000</value>
         </textValue>
      </text>
   </variable>
</deviceState>

我想实现一个功能,我可以通过指定搜索过滤器来访问每个变量id的值,然后直接访问它的值,而不会被以下无意义的标记所困扰。

<text><textValue> or <text><textDate> or <text><textStr/> or <text>
<textvalue><value>
等等,让我们说new devices {a.id=node("id"), a.value=node("valu")}

目前我正在使用以下代码来实现这一技巧,但它的速度和可管理性都​​不高效。

XDocument x = XDocument.Parse(_back);
            string back = "";
                string xvalue, yvalue;
                foreach (XElement xe in x.Descendants().Take(1))
                {

                    var s = xe.Value.IndexOf("Plug - Meter.VI");
                    var val = xe.Value.Substring(s, 25);
                     back= val.Substring(val.LastIndexOf("I")+1, 10);
                break;
                }

任何指导都将受到高度赞赏。感谢

根据Monty的反馈,这就是我正在使用的。

public protoDCM_CornerL08UMS_View()
         {
             InitializeComponent();
             synchronizationContext = SynchronizationContext.Current;

             StartingMeter();

         }


  private async void StartingMeter()
         {
             string val="0";
             max = min = 0;

             await Task.Run(() =>
             {
                 do
                 {
                     UpdateUI(val,max,min);
                     val = fetchData();
                     Double temp =0;
                     if (Double.TryParse(val,out temp))
                     {
                         if(min==0&&max==0)
                         {
                             min = max = temp;
                         }
                         if(temp>max)
                         {
                             max = temp;
                         }
                         if(temp<min)
                         {
                             min = temp;
                         }

                     }
                     val = temp.ToString();
                 }
                 while (true);

             });
         }

  private void UpdateUI(string value, Double _max , Double _min)
         {
             var timeNow = DateTime.Now;

             if ((DateTime.Now - previousTime).Milliseconds <= 50) return;

             synchronizationContext.Post(new SendOrPostCallback(o =>
             {
                 lblInstant.Text= (string)o;
             }), value);

             synchronizationContext.Post(new SendOrPostCallback(o =>
             {
                 lblMax.Text = (string)o;
             }), _max.ToString());



             synchronizationContext.Post(new SendOrPostCallback(o =>
             {
                 lblMin.Text = (string)o;
             }), _min.ToString());

             previousTime = timeNow;
         }

  public Stream GenerateStreamFromString(string s)
                 {
                     MemoryStream stream = new MemoryStream();
                     StreamWriter writer = new StreamWriter(stream);
                     writer.Write(s);
                     writer.Flush();
                     stream.Position = 0;
                     return stream;
                 }

         private string fetchData()
         {


                 String _back = HttpGet("http://xxx.xxx.xxx"+Global.Node.Current.Device.Address+"/services/devices/deviceState.xml?id=D83AE139-E0C9-4B15-B2A9-6E0B57B28ED1?type=ALL");
             //_back = FormatXML(Response);
             try
             {
                 DeviceState deserializedXML = new DeviceState();

                 XmlSerializer serializer = new XmlSerializer(typeof(DeviceState));
                 using (Stream stream = GenerateStreamFromString(_back))
                 {
                     deserializedXML = (DeviceState)serializer.Deserialize(stream);

                     var x = (from z in deserializedXML.Variable
                              where z.Id == "Plug - Meter.VI"
                              select new
                              {
                                  Id = z.Id,
                                value= (z.Text.TextDate == null?
                               (z.Text.TextStr == null
                               ? (z.Text.TextValue == null
                               ? "No Text Value!" : z.Text.TextValue.Value.ToString())  : z.Text.TextStr.ToString()) : z.Text.TextDate.Date.ToString())  }).FirstOrDefault();

                     return x.value;

                 } 
             }
             catch (Exception w)
             {
                 MessageBox.Show(w.ToString());
                 return "0.0";
             }
         }

   public static string HttpGet(string URI)
         {
             try
             {

                 System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
                 req.Method = "GET";
                System.Net.WebResponse resp = req.GetResponse();
                 System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
                 return sr.ReadToEnd().Trim();
             }
             catch (Exception sl)
             {
return sl.ToString();
             }
         }

1 个答案:

答案 0 :(得分:1)

试试这个......

Usings

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

[XmlRoot(ElementName = "textValue")]
public class TextValue
{
    [XmlElement(ElementName = "value")]
    public string Value { get; set; }
}

[XmlRoot(ElementName = "text")]
public class Text
{
    [XmlElement(ElementName = "textValue")]
    public TextValue TextValue { get; set; }
    [XmlElement(ElementName = "textStr")]
    public string TextStr { get; set; }
    [XmlElement(ElementName = "textDate")]
    public TextDate TextDate { get; set; }
}

[XmlRoot(ElementName = "variable")]
public class Variable
{
    [XmlElement(ElementName = "id")]
    public string Id { get; set; }
    [XmlElement(ElementName = "text")]
    public Text Text { get; set; }
}

[XmlRoot(ElementName = "textDate")]
public class TextDate
{
    [XmlElement(ElementName = "date")]
    public string Date { get; set; }
}

[XmlRoot(ElementName = "deviceState")]
public class DeviceState
{
    [XmlElement(ElementName = "id")]
    public string Id { get; set; }
    [XmlElement(ElementName = "device")]
    public string Device { get; set; }
    [XmlElement(ElementName = "variable")]
    public List<Variable> Variable { get; set; }
}

        try
        {
            DeviceState deserializedXML = new DeviceState();
            // Deserialize to object
            XmlSerializer serializer = new XmlSerializer(typeof(DeviceState));
            using (FileStream stream = File.OpenRead(@"xml.xml"))
            {
                deserializedXML = (DeviceState)serializer.Deserialize(stream);

                // Now get all your IDs
                List<string> IDs = (from xml in deserializedXML.Variable select xml.Id).ToList();
            } // Put a break-point here, then mouse-over IDs and you will see all your IDs... deserializedXML contains the entire object if you want anythin else ....
        }
        catch (Exception)
        {
            throw;
        }

我从应用程序* .exe文件夹中的文件(xml.xml)中读取XML,您需要根据具体要求调整此解决方案....

这是你需要的吗?....

        try
        {
            XmlSerializer DeserializerPlaces = new XmlSerializer(typeof(DeviceState));

            string html = string.Empty;
            string url = @"https://<Your URL>";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //request.AutomaticDecompression = DecompressionMethods.GZip;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                DeviceState dezerializedXML = (DeviceState)DeserializerPlaces.Deserialize(reader);
                //html = reader.ReadToEnd();
            }
            //Console.WriteLine(html);

        }
        catch (System.Exception)
        {
            throw;
        }

您可以每隔5秒调用一次并更新您的UI(来自dezerializedXML属性)