C# 从C语言中的SQLite数据库读取具有名称空间的xml数据
我遇到了一个来自具有两个名称空间的SQLite数据库的XML结构。当不存在属性名称时,如何在第二个ns后面引用“floatnumber”C# 从C语言中的SQLite数据库读取具有名称空间的xml数据,c#,xml,linq,sqlite,C#,Xml,Linq,Sqlite,我遇到了一个来自具有两个名称空间的SQLite数据库的XML结构。当不存在属性名称时,如何在第二个ns后面引用“floatnumber” <?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/XMLS
<?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>
我在这里的第一篇文章。
提供的XML anyType中的Tom没有命名空间。它确实定义了两个名称空间q1和d1p1,但它们不用于引用元素。下面的示例使用。你也可以使用
更新
使用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;
}
}
}
谢谢您,Rich,但是任何类型的[content='floatnumber']都不起作用,因为'floatnumber'总是被一个以KB为单位表示SharePoint备份大小的数字所占用。很抱歉,我可能没有充分描述该元素。将字符串转换为double时出现NullReferenceException,因为's'为NullOrEmpty。
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);
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;
}
}
}