我无法使用 XElement 获取 <pmlcore:Sensor
I can't get <pmlcore:Sensor with XElement
我正在使用 .NET Framework 4.5.1 开发 ASP.NET MVC 应用程序,returns XML 从数据库数据生成。
我想获得这个:
<?xml version="1.0" encoding="utf-8"?>
<pmlcore:Sensor [ Ommitted for brevety ] ">
但我明白了:
<?xml version="1.0" encoding="utf-8"?>
<Sensor [ Ommitted for brevety ] xmlns="pmlcore">
阅读了在 Whosebug 中找到的所有答案,我更改了我的代码以使用 XNamespace
:
XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
XDeclaration dec = new XDeclaration("1.0", "utf-8", null);
XNamespace pmlcore = "pmlcore";
XNamespace pmluid = "pmluid";
root = new XElement(pmlcore + "Sensor"
, new XAttribute(XNamespace.Xmlns + "pmluid",
"urn:autoid:specification:universal:Identifier:xml:schema:1")
, new XAttribute(XNamespace.Xmlns + "xsi", ns)
, new XAttribute(XNamespace.Xmlns + "pmlcore",
"urn:autoid:specification:interchange:PMLCore:xml:schema:1")
, new XAttribute(ns + "noNamespaceSchemaLocation",
"urn:autoid:specification:interchange:PMLCore:xml:schema:1 ./PML/SchemaFiles/Interchange/PMLCore.xsd")
如何获得<pmlcore:Sensor
这个?
如果我使用此代码:
root = new XElement("pmlcore:Sensor"
我收到这个错误:
The ':' character, hexadecimal value 0x3A, cannot be included in a
name
问题是您添加了错误的命名空间...您正在尝试为其使用 别名,而不是命名空间 URI。这是一个有效的具体示例:
using System;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
XNamespace pmlcore = "urn:autoid:specification:interchange:PMLCore:xml:schema:1";
XNamespace pmluid = "urn:autoid:specification:universal:Identifier:xml:schema:1";
var root = new XElement(pmlcore + "Sensor",
new XAttribute(XNamespace.Xmlns + "pmluid", pmluid.NamespaceName),
new XAttribute(XNamespace.Xmlns + "pmlcore", pmlcore.NamespaceName));
Console.WriteLine(root);
}
}
输出(重新格式化):
<pmlcore:Sensor
xmlns:pmluid="urn:autoid:specification:universal:Identifier:xml:schema:1"
xmlns:pmlcore="urn:autoid:specification:interchange:PMLCore:xml:schema:1" />
我正在使用 .NET Framework 4.5.1 开发 ASP.NET MVC 应用程序,returns XML 从数据库数据生成。
我想获得这个:
<?xml version="1.0" encoding="utf-8"?>
<pmlcore:Sensor [ Ommitted for brevety ] ">
但我明白了:
<?xml version="1.0" encoding="utf-8"?>
<Sensor [ Ommitted for brevety ] xmlns="pmlcore">
阅读了在 Whosebug 中找到的所有答案,我更改了我的代码以使用 XNamespace
:
XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
XDeclaration dec = new XDeclaration("1.0", "utf-8", null);
XNamespace pmlcore = "pmlcore";
XNamespace pmluid = "pmluid";
root = new XElement(pmlcore + "Sensor"
, new XAttribute(XNamespace.Xmlns + "pmluid",
"urn:autoid:specification:universal:Identifier:xml:schema:1")
, new XAttribute(XNamespace.Xmlns + "xsi", ns)
, new XAttribute(XNamespace.Xmlns + "pmlcore",
"urn:autoid:specification:interchange:PMLCore:xml:schema:1")
, new XAttribute(ns + "noNamespaceSchemaLocation",
"urn:autoid:specification:interchange:PMLCore:xml:schema:1 ./PML/SchemaFiles/Interchange/PMLCore.xsd")
如何获得<pmlcore:Sensor
这个?
如果我使用此代码:
root = new XElement("pmlcore:Sensor"
我收到这个错误:
The ':' character, hexadecimal value 0x3A, cannot be included in a name
问题是您添加了错误的命名空间...您正在尝试为其使用 别名,而不是命名空间 URI。这是一个有效的具体示例:
using System;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
XNamespace pmlcore = "urn:autoid:specification:interchange:PMLCore:xml:schema:1";
XNamespace pmluid = "urn:autoid:specification:universal:Identifier:xml:schema:1";
var root = new XElement(pmlcore + "Sensor",
new XAttribute(XNamespace.Xmlns + "pmluid", pmluid.NamespaceName),
new XAttribute(XNamespace.Xmlns + "pmlcore", pmlcore.NamespaceName));
Console.WriteLine(root);
}
}
输出(重新格式化):
<pmlcore:Sensor
xmlns:pmluid="urn:autoid:specification:universal:Identifier:xml:schema:1"
xmlns:pmlcore="urn:autoid:specification:interchange:PMLCore:xml:schema:1" />