[英]Namespaces, Schemas, Elements and Attributes in an XmlDocument in .NET
我把这个放在这里是因为我在 StackOverflow 上看到了很多关于 XML 的问答,同时试图解决我自己的问题,并认为一旦我找到它,我就会发布我发现的内容,所以当其他人需要一些 XML 帮助时,这可能对他们有帮助。
我的目标:创建一个 XML 文档,其中包含以下 XML 声明、架构和命名空间信息:
<?xml version="1.0" encoding="UTF-8"?>
<abc:abcXML xsi:schemaLocation="urn:abcXML:v12 http://www.test.com/XML/schemas/v12/abcXML_v12.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ase="urn:abcXML:v12">
我已经在 Python 中使用minidom进行了快速原型制作,而且非常简单。 我需要用 .NET 语言( C# )来做,因为这就是业务所需要的。 我对 C# 非常熟悉,但我一直远离用它处理 XML,因为老实说,我对 XML 和它的指南没有深入的了解。 今天,我不得不面对我的恶魔。
我是这样做的:
第一部分很简单——创建一个文档,并为根创建一个 DocumentElement(这里有一个问题,我稍后会谈到):
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
下一部分似乎很简单——创建一个元素,给它一个前缀、名称和 URI,然后将 append 放到文档中。 我认为这会起作用,但它不起作用(这是对 XML 的最小理解发挥作用的地方):
XmlElement abcXML = xmlDoc.CreateElement("ase", "abcXML", "urn:abcXML:r38 http://www.w3.org/2001/XMLSchema-instance");
XmlAttribute xmlAttr = xmlDoc.CreateAttribute("xsi:schemaLocation", "urn:abcXML:v12 http://www.test.com/XML/schemas/v12/abcXML_v12.xsd");
abcXML.AppendChild(xmlAttr);
xmlDoc.AppendChild(abcXML);
我尝试使用doc.LoadXml()
和doc.CreateDocumentFragment()
并编写自己的声明。 不 - 我会得到“文件意外结束”。 对于那些对XmlDocumentFragment
感兴趣的人: https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmldocumentfragment.innerxml?view=netcore-3.1
这篇关于 XML 架构和命名空间的 Microsoft 文章并没有直接帮助我: https://docs.microsoft.com/en-us/dotnet/standard/data/xml/including-or-importing-xml-schemas
在对 XML 进行了更多阅读后,并浏览了XmlDocument
、 XmlElement
和XmlAttribute
的文档,这就是解决方案:
XmlElement abcXML = xmlDoc.CreateElement("ase", "abcXML", "urn:abcXML:r38");
XmlAttribute xmlAttr = xmlDoc.CreateAttribute("xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
xmlAttr.InnerXml = "urn:abcXML:v12 http://www.test.com/XML/schemas/v12/abcXML_v12.xsd";
abcXML.Attributes.Append(xmlAttr);
xmlDoc.AppendChild(abcXML);
现在您可以像这样将元素添加到文档中:
XmlElement header = doc.CreateElement(string.Empty, "Header", string.Empty);
abcXML.AppendChild(header);
为了保存文档,我使用了:
xmlDoc.Save(fileLocation);
我将我的 output 与我的样本进行了比较,在比较文件内容后,我成功匹配了它。 我向客户提供了 output ,他们将其上传到他们正在使用的应用程序中,但失败了: Row 1, Column 1 - Unexpected Character
。
我怀疑它在编码,我是对的。 Using xmlDoc.Save(fileLocation)
is correct, but it generates a UTF-8 file with the Byte Order Mark (BOM) at Row 1, Column 1. The XML parsing function in the application doesn't expect that, so the process failed. 为了解决这个问题,我使用了以下方法:
Encoding enc = new UTF8Encoding(false); /* This creates a UTF-8 encoding without the BOM */
using (System.IO.TextWriter tw = new System.IO.StreamWriter(filePath, false, enc))
{
xmlDoc.Save(tw);
}
return true;
我再次生成文件,将其发送给客户端,它首先工作 go。
我希望有人发现这很有用。
对于复杂的命名空间,只解析 xml 字符串会更简单。 我喜欢使用 xml linq。 您的样品 xml 是错误的。 命名空间是“ase”(不是 abc)。
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 xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ase:abcXML xsi:schemaLocation=\"urn:abcXML:v12 http://www.test.com/XML/schemas/v12/abcXML_v12.xsd\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:ase=\"urn:abcXML:v12\">" +
"</ase:abcXML>";
XDocument doc = XDocument.Parse(xml);
XElement root = doc.Root;
XNamespace nsAse = root.GetNamespaceOfPrefix("ase");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.