【问题标题】:Read xml data with namespaces from SQLite database in C#从 C# 中的 SQLite 数据库中读取带有命名空间的 xml 数据
【发布时间】:2015-10-04 07:33:55
【问题描述】:

我遇到了来自具有两个命名空间的 SQLite 数据库的 XML 结构。当没有属性名称时,如何引用“floatnumber”(第二个 ns 之后)?

<?xml version="1.0" encoding="utf-8"?>
<anyType xmlns:q1="http://www.w3.org/2001/XMLSchema"
         d1p1:type="q1:string" 
         xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance">
   floatnumber
 </anyType>

使用包 v1.0.98.0 从https://system.data.sqlite.org/ 连接到 SQLite 数据库非常简单。我在玩 XmlReader 和 XDocument (LINQ-to-XML) 但没有成功。

在 C# 中从 xml 读取“floatnumber”(数据库列“值”)的最佳方法是什么?

using (SQLiteCommand sqlcmd = new SQLiteCommand())
{
    sqlcmd.Connection = connect;
    sqlcmd.CommandText = sqlquery;
    SQLiteDataReader rdr = null;
    try
    {
        rdr = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (rdr.Read())
        {
            string xmlstr = rdr["Value"].ToString();
            Console.WriteLine(xmlstr);                  // gives whole xml structure

            /* code to get floatnumber from xml */
        }
    }
    catch ()
    {}
}

我在这里的第一篇文章。 汤姆

【问题讨论】:

    标签: c# xml linq sqlite


    【解决方案1】:

    使用 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
        {
            static void Main(string[] args)
            {
                string xmlstr =
                    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                    "<anyType xmlns:q1=\"http://www.w3.org/2001/XMLSchema\"" +
                          " d1p1:type=\"q1:string\"" +
                          " xmlns:d1p1=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                          "floatnumber" +
                   "</anyType>";
    
                XDocument doc = XDocument.Parse(xmlstr);
                XElement anyType = (XElement)doc.FirstNode;
                XNamespace ns = anyType.Name.Namespace;
                XNamespace q1 = anyType.Attributes().Where(x => x.Name.LocalName == "q1").FirstOrDefault().Name.Namespace;
                XNamespace type = anyType.Attributes().Where(x => x.Name.LocalName == "type").FirstOrDefault().Name.Namespace;
                XNamespace d1p1 = anyType.Attributes().Where(x => x.Name.LocalName == "d1p1").FirstOrDefault().Name.Namespace;
    
                string floatnumber = anyType.Value;
    
            }
        }
    }​
    

    【讨论】:

      【解决方案2】:

      从提供的 XML anyType 中,不 有命名空间。它确实定义了两个命名空间q1 和d1p1,但它们不用于引用元素。下面的示例使用XPath expression 来获取元素。你也可以使用Linq to XML。

      Using System.Xml;
      
      var doc = new XmlDocument();
      doc.LoadXml(xmlstr);
      var floatnumber = doc.SelectSingleNode("anyType[content() = 'floatnumber']");
      

      更新

      string s = doc.SelectSingleNode("anyType").Value;
      double d = XmlConvert.ToDouble(s);
      

      【讨论】:

      • 谢谢 Rich,“但是 anyType[content() = 'floatnumber']”将不起作用,因为 'floatnumber' 始终被一个数字所占据,该数字以千字节为单位表示 SharePoint 备份的大小。抱歉,可能是我描述的不够充分。
      • NullReferenceException 将字符串转换为双精度,因为 's' IsNullOrEmpty。
      猜你喜欢
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2018-09-16
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多