【问题标题】:How to show a specific part of XML File?如何显示 XML 文件的特定部分?
【发布时间】:2020-07-03 01:54:36
【问题描述】:

我正在寻找一种在 XML 文件中显示特定代码部分的方法。 我在 xml 文件中备份了 SMS,我正在尝试编写一个软件来显示来自一个联系人的所有消息(在 TextBox 中选择) (对我来说)最大的问题是所有信息(联系人姓名、消息、电话号码)都在一个标签内。

这里是一个xml文件的例子:

  <sms protocol="0" address="phone number" date="1338793176487" type="1" subject="null" body="SMS text" toa="null" sc_toa="null" service_center="phone number" read="1" status="-1" locked="0" date_sent="null" readable_date="4 juin 2012 08:59:36" contact_name="contact name" />

PS:我正在使用带有 .NET Framework 的 Visual Studio

【问题讨论】:

  • 您要搜索的属性是什么?联系人姓名、电话号码、...?
  • 我想搜索联系人,然后选择联系人的每条消息
  • 再次,联系人如何识别自己?你想搜索contact_name="contact name"。但是可能有几个人同名。因此address="phone number" 似乎可以更好地识别联系人。您是否尝试过我提出的任何解决方案?

标签: c# xml visual-studio


【解决方案1】:

使用 Xml Linq 将所有属性放入字典中:

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

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

            XElement sms = XElement.Parse(xml);

            Dictionary<string, string> dict = sms.Attributes().GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}

【讨论】:

    【解决方案2】:

    您可以使用 [] 在 XPath 中使用 XmlDocument 指定条件,如下所示:

    //sms[@address="..."]
    

    这意味着:选择所有sms 节点,条件是@address 的值为...。 @-符号表示一个属性。

    代码示例:

    string xml = "<sms protocol=\"0\" address=\"phone number\" date=\"1338793176487\" type=\"1\" subject=\"null\" body=\"SMS text\" toa=\"null\" sc_toa=\"null\" service_center=\"phone number\" read=\"1\" status=\"-1\" locked=\"0\" date_sent=\"null\" readable_date=\"4 juin 2012 08:59:36\" contact_name=\"contact name\" />";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    var nodes = doc.SelectNodes("//sms[@address=\"phone number\"]");
    foreach(var node in  nodes)
    {
        Console.WriteLine(node);
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用带有XDocument 的Linq to XML 并使用where 子句来选择具有特定属性集的节点。

      代码示例:

      string xml = "<sms protocol=\"0\" address=\"phone number\" date=\"1338793176487\" type=\"1\" subject=\"null\" body=\"SMS text\" toa=\"null\" sc_toa=\"null\" service_center=\"phone number\" read=\"1\" status=\"-1\" locked=\"0\" date_sent=\"null\" readable_date=\"4 juin 2012 08:59:36\" contact_name=\"contact name\" />";
      var doc = XDocument.Parse(xml);
      
      var nodes = from element in doc.Elements("sms") 
                  where (string) element.Attribute("address") == "phone number"
                  select element;
      foreach(var node in  nodes)
      {
          Console.WriteLine(node);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-08
        • 1970-01-01
        • 2012-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-29
        相关资源
        最近更新 更多