- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从数据库中获取一些字符串形式的 XML 数据。数据以ntext形式保存在数据库中。
从数据库获取数据不是问题。问题是稍后,当我想处理xml中的数据时。我将字符串加载到 xDocument 中。
我想首先获取 Owner 值。但是我得到了一个空引用异常,这意味着我没有编写我假设的正确的 Xpath。
写“./Owner”不起作用。写“/./Owner”不起作用,然后我得到一个 XML 异常。
我开始使用 XMLDocument,但我认为遇到了命名空间问题。开始阅读,看起来使用 xDocument 更好。正如您还可以通过我的代码看到的,我尝试通过两种方式获取 Owner 值,但都失败了。
我的 XML 看起来有点像这样:
<Container xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="DM">
<IsConsigned>false</IsConsigned>
<LockState>Unlocked</LockState>
<SourceType i:nil="true" />
<Id>04216194-4f62-47ee-ab21-c1053d01bf1e</Id>
<Owner>IN</Owner>
<Created>2012-08-21T09:29:10.528321+02:00</Created>
</Container>
我的代码:
class Program
{
static SqlConnection conn = new SqlConnection();
static XDocument xml = new XDocument();
static void Main(string[] args)
{
using (conn)
{
conn.ConnectionString = Properties.Settings.Default.connectionString;
//connection.Open();
conn.Open();
SqlDataReader reader = GetDataFromDatabase();
if (reader.HasRows)
{
while (reader.Read())
{
string xmlFile = reader.GetSqlString(0).ToString();
HandleData(xmlFile);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}
public static void HandleData(string xmlIn)
{
xml = XDocument.Parse(xmlIn);
XElement xmlElement = xml.Root;
string test1 = xml.Element("Owner").Value.ToString();
string test = xmlElement.Element("Owner").Value.ToString();
}
}
最佳答案
这不是使用 XmlDocument
的问题或XDocument
。您的 XML 有默认命名空间,这是一个没有前缀声明的命名空间,在这里:
xmlns="DM"
与前缀 i
的对比这里:xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
。请注意,不仅声明默认命名空间的元素位于该命名空间中,而且所有后代元素都隐式继承祖先默认命名空间,除非另有指定(使用指向不同命名空间 uri 的显式命名空间前缀或本地默认命名空间)。
您可以使用 "XNamespace"+"element's local name"
的组合形成一个限定名称来引用命名空间中的元素,例如:
var xmlIn = @"<Container xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='DM'>
<IsConsigned>false</IsConsigned>
<LockState>Unlocked</LockState>
<SourceType i:nil='true' />
<Id>04216194-4f62-47ee-ab21-c1053d01bf1e</Id>
<Owner>IN</Owner>
<Created>2012-08-21T09:29:10.528321+02:00</Created>
</Container>";
var xml = XDocument.Parse(xmlIn);
XNamespace d = "DM";
string owner = xml.Root.Element(d+"Owner").Value;
Console.WriteLine(owner);
string id = xml.Root.Element(d+"Id").Value;
Console.WriteLine(id);
输出:
IN
04216194-4f62-47ee-ab21-c1053d01bf1e
旁注:
Element()
无法识别 XPath 表达式,它接受 XName
参数代替。XElement.Value
property 已经是一个字符串,不需要调用 ToString()
就在上面。XDocument
的 XPath后来:Use XPath with XML namespace 关于c# - 空引用异常 - 使用 Element 和 XPath 的 XDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31532419/
我有一个将 XDocument 对象作为参数的函数。 我需要遍历不同集合中的许多其他对象,并针对这些对象中的每一个,在 XDocument 上执行一些操作。但是 lopp 的每次迭代都需要传递给函数的
下面的代码可以工作,但是很乱而且很慢。我正在使用带有 Saxon 的 XSLT2 将一个 XDocument 转换为另一个 XDocument,使用 SaxonWrapper 进行改编: public
我在 Visual Studio 2010 的 MVC4 Web API 应用程序中使用 XDocument,但不确定测试策略。 我的大部分单元测试都使用内存中的 XDocument,它适用于 Con
我无法使用 Linq 加载新的 XML 文档。那是我的代码: using System; using System.Collections.Generic; using System.Linq.Exp
抱歉,如果我的问题不清楚。我从 xDocument 中获得了一堆元素和每个元素的属性。现在我希望在每次迭代中获得每个节点的更多后代并遍历它们并获取它们的所有属性。 结构:
我接管了一个以二进制格式在 SQL Server 中存储大型 XML 文档的系统。 目前,数据是通过将其转换为字符串,然后将该字符串转换为字节数组来保存的。但最近在尝试转换为字符串时遇到一些大型 XM
我有一个使用 XDocument 构建 xml 文档的小应用程序。但是,一段时间后,该应用程序使用了超过 1GB 的内存。 所以我想知道是否有办法让 XDocument 使用磁盘而不是内存。例如,通过
我有一个像这样的 XDocument: 1057 Lookup_test open 1057 Lookup_test Waiting for input
如何将 xml 元素导入到对象?我下面的代码不起作用,它在 SetValue 处失败,我不明白为什么。 但即便如此,我怀疑 linq 有一种更简洁的方法来执行此操作,但我找不到任何示例。 class
我多年来一直通过 API 访问数据库,今天进行了更改,但我无法联系所有者。这似乎是一个微妙的变化,导致我的代码给出空引用异常。下载一个文件,然后我尝试使用 XmlReader 并使用以下代码将其加载到
我正在尝试将 XDocument 反序列化为包含数组/列表的类。问题是这段代码没有这样做,我总是得到一个没有任何库存项目的 Response 类(不是 null,但计数 = 0) 这是我的测试方法:
我正在使用 XDocument 来保存某种数据库。这个数据库由注册的聊天机器人组成,我只是有许多具有“用户名”、“所有者”等属性的“机器人”节点。然而,偶尔一些聪明人决定制作一个具有非常奇怪角色作为属
我有一个简化后的 xml 文档,如下所示: 有一个\t作为属性值。 执行这段代码时 var path = @"C:\test.xml"; var doc = XDocument.Load(path
当我尝试使用下一个代码加载一些 XML 文件时: XDocument configuration = XDocument.Load("%NAME_OF_THE_PATH%/Default_Te
我已经完成了研究并尝试了很多次试验,但这让我感到难过。我可能有点笨,但是鉴于以下 XML 文档,我怎样才能最轻松地找到 UnitTestResult 元素?
我需要将多个 namespace 添加到具有相同地址的 xdocument (C#) stuff and more stuff 如果我使用下面的代码添加..它只显示 xmlns:f XNames
如何从加载的 XDocument 中删除特定节点?我的 XML 文档如下所示: if (condition) { } ...
我有一个类似于 的 XDocument 我希望将属性 foo
很容易找到 Xdocument 属性的值,但是我们如何找到带有前缀/命名空间的属性。 XML 代码 My value 我正在尝试读取属性值 xlink:Id(其中 p 是 XElement) p =
我有以下 XML 文档: 我正在尝试使用 XDocument 解析此 XML,以便获得包含节点及其值的列表(字典): source => mysource,name
我是一名优秀的程序员,十分优秀!