如何解析这个Xml类型字符串

发布于 2024-11-01 02:14:49 字数 505 浏览 1 评论 0原文

有人可以指导我如何解析这个 Xml 类型字符串吗?

<data>
  <LastUpdate></LastUpdate>
  <AC1>12</AC1>
  <AC2>13</AC2>
  <AC3>14</AC3>
  <Moter></Moter>
  <Fan1></Fan1>
  <Fan2></Fan2>
  <TubeLight1></TubeLight1>
  <TubeLight2></TubeLight2>
  <Moter></Moter>
  <CloseAll></CloseAll>
</data>

我需要获取字符串或列表或字典中的所有结果,例如 AC1=12 , AC2=13 等等

提前谢谢

Can some one guide me how to parse this Xml type string ?

<data>
  <LastUpdate></LastUpdate>
  <AC1>12</AC1>
  <AC2>13</AC2>
  <AC3>14</AC3>
  <Moter></Moter>
  <Fan1></Fan1>
  <Fan2></Fan2>
  <TubeLight1></TubeLight1>
  <TubeLight2></TubeLight2>
  <Moter></Moter>
  <CloseAll></CloseAll>
</data>

I need to get all result in String or List or dictionary like
AC1=12 , AC2=13 and so on

Thnaks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

々眼睛长脚气 2024-11-08 02:14:49

使用 XDocument.Parse 方法:

string data = @"<data>
                  <LastUpdate></LastUpdate>
                  <AC1>12</AC1>
                  <AC2>13</AC2>
                  <AC3>14</AC3>
                  <Moter></Moter>
                  <Fan1></Fan1>
                  <Fan2></Fan2>
                  <TubeLight1></TubeLight1>
                  <TubeLight2></TubeLight2>
                  <Moter></Moter>
                  <CloseAll></CloseAll>
            </data>";

XDocument xmlDoc = XDocument.Parse(data);

var parsedData = from obj in xmlDoc.Descendants("data")
                 select new
                 {
                     LastUpdate = obj.Element("LastUpdate").Value,
                     AC1 = obj.Element("AC1").Value,
                     AC2 = obj.Element("AC1").Value,
                     ... and so on
                 }

好运气!

Use XDocument.Parse method:

string data = @"<data>
                  <LastUpdate></LastUpdate>
                  <AC1>12</AC1>
                  <AC2>13</AC2>
                  <AC3>14</AC3>
                  <Moter></Moter>
                  <Fan1></Fan1>
                  <Fan2></Fan2>
                  <TubeLight1></TubeLight1>
                  <TubeLight2></TubeLight2>
                  <Moter></Moter>
                  <CloseAll></CloseAll>
            </data>";

XDocument xmlDoc = XDocument.Parse(data);

var parsedData = from obj in xmlDoc.Descendants("data")
                 select new
                 {
                     LastUpdate = obj.Element("LastUpdate").Value,
                     AC1 = obj.Element("AC1").Value,
                     AC2 = obj.Element("AC1").Value,
                     ... and so on
                 }

Good luck!

放血 2024-11-08 02:14:49

这应该可以工作,但您必须从 XML 中删除重复的 Moter 元素 - 只有这样您才能使用字典:

XDocument doc = XDocument.Load("test.xml");
var dictionary = doc.Descendants("data")
                    .Elements()
                    .ToDictionary(x => x.Name.ToString(), x => x.Value);
string ac1Value = dictionary["AC1"];

This should work but you have to remove the duplicate Moter element from your XML - only then you can use a dictionary:

XDocument doc = XDocument.Load("test.xml");
var dictionary = doc.Descendants("data")
                    .Elements()
                    .ToDictionary(x => x.Name.ToString(), x => x.Value);
string ac1Value = dictionary["AC1"];
懷念過去 2024-11-08 02:14:49

如果您想使用 Linq to XML,那么它看起来会是这样的:

        XElement root = XElement.Parse(s);
        Dictionary<XName, string> dict = root
            .Elements()
            .Select(x => new {key = x.Name, value = x.Value})
            .ToDictionary(x => x.key, x => x.value);

只要确保按照您想要的方式处理重复项即可。

If you wanted to go for Linq to XML then it would look something like:

        XElement root = XElement.Parse(s);
        Dictionary<XName, string> dict = root
            .Elements()
            .Select(x => new {key = x.Name, value = x.Value})
            .ToDictionary(x => x.key, x => x.value);

Just make sure you deal with duplicates the way you want.

人生戏 2024-11-08 02:14:49

我更喜欢使用 XLinq。这是示例(在 VB.NET 中):

 Private Sub ParseIt()

        Dim xml = XElement.Parse(sampleXml)

        Dim dic As New Dictionary(Of String, String)

        For Each item In xml.Elements
            dic.Add(item.Name.LocalName, item.Value)
        Next

    End Sub

您也可以像这样使用它(我更喜欢这种方法):

Private Sub ParseIt()

    Dim xml = XElement.Parse("")

    Dim dic = (From item In xml.Elements).ToDictionary(Function(obj) obj.Name.LocalName, Function(obj) obj.Value)

End Sub

I prefer using XLinq. Here is the sample (in VB.NET):

 Private Sub ParseIt()

        Dim xml = XElement.Parse(sampleXml)

        Dim dic As New Dictionary(Of String, String)

        For Each item In xml.Elements
            dic.Add(item.Name.LocalName, item.Value)
        Next

    End Sub

Also you can use it like this (I prefer this method):

Private Sub ParseIt()

    Dim xml = XElement.Parse("")

    Dim dic = (From item In xml.Elements).ToDictionary(Function(obj) obj.Name.LocalName, Function(obj) obj.Value)

End Sub
黯然 2024-11-08 02:14:49

如果您想将 xml 数据字符串解析为 'Dataset' 那么您可以使用此示例

    string xmlString = @"/*.. .. .*/";

    DataSet data = new DataSet();

    data.ReadXml(new StringReader(xmlString));

If you want to parse xml data string into 'Dataset' then you can use this sample

    string xmlString = @"/*.. .. .*/";

    DataSet data = new DataSet();

    data.ReadXml(new StringReader(xmlString));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文