我想将下面的标记写入XML文件中。
<StructureMetaData xsi:schemaLocation="MSD" xmlns="MSD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- here a following more tags -->
</StructureMetaData>对于这些,我做以下几点
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t"
CheckCharacters = false,
};
XmlWriter xmlWriter = XmlWriter.Create (filename, settings);
xmlWriter.WriteStartDocument ();
xmlWriter.WriteStartAttribute ("xsi", "schemaLocation", "MSD");
xmlWriter.WriteStartAttribute ("xmlns", "MSD");
xmlWriter.WriteStartAttribute ("xmlns", "xsi", "http://www.w3.org/2001/XMLSchema-instance");但是,在第一次调用WriteStartAttribute之后,我得到了以下例外:
“状态文档中的令牌StartAttribute将导致无效的XML文档。”
如何将这些属性写入xml标记?
发布于 2017-08-08 19:46:13
你有几个问题:
<StructureMetaData first。有一个默认的名称空间xmlns="MSD",因此您需要在该名称空间中编写它。XmlWriter.WriteStartAttribute(),但是此方法只写入属性名称。您仍然必须在名称之后编写值,例如,使用XmlWriter.WriteString()。或者,您可以通过对XmlWriter.WriteAttributeString()的一次调用来编写名称和值。xmlWriter语句关闭并释放您的using。下面是使用WriteStartAttribute()的代码的工作版本
using (var xmlWriter = XmlWriter.Create(filename, settings))
{
xmlWriter.WriteStartDocument();
var rootNamespace = "MSD";
// Write the root element in the MSD namespace
xmlWriter.WriteStartElement("StructureMetaData", rootNamespace);
// Write the root element's attribute names and values
xmlWriter.WriteStartAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
xmlWriter.WriteString("MSD");
xmlWriter.WriteStartAttribute("xmlns");
xmlWriter.WriteString(rootNamespace);
xmlWriter.WriteStartAttribute("xmlns", "xsi", null);
xmlWriter.WriteString("http://www.w3.org/2001/XMLSchema-instance");
// Write the root element contents
// Close the root element
xmlWriter.WriteEndElement();
// Close the document
xmlWriter.WriteEndDocument();
}和使用WriteAttributeString()的更简单的版本
using (var xmlWriter = XmlWriter.Create(filename, settings))
{
xmlWriter.WriteStartDocument();
var rootNamespace = "MSD";
// Write the root element in the MSD namespace
xmlWriter.WriteStartElement("StructureMetaData", rootNamespace);
// Write the root element's attribute names and values
xmlWriter.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "MSD");
xmlWriter.WriteAttributeString("xmlns", rootNamespace);
xmlWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
// Write the root element contents
// Close the root element
xmlWriter.WriteEndElement();
// Close the document
xmlWriter.WriteEndDocument();
}示例.Net小提琴显示这两个选项。
发布于 2017-08-08 14:57:54
您必须编写一个开始元素,在该元素上写入属性:
xmlWriter.WritestartElement("StructureMetaData");
xmlWriter.WriteStartAttribute(...);发布于 2017-08-08 15:12:46
使用xml更容易:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication72
{
class Program
{
static void Main(string[] args)
{
string root = "<StructureMetaData xsi:schemaLocation=\"MSD\" xmlns=\"MSD\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></StructureMetaData>";
XDocument doc = XDocument.Parse(root);
XElement metaData = doc.Root;
XNamespace ns = metaData.GetDefaultNamespace();
metaData.Add(new XElement(ns + "ABC", new object[] {
new XElement(ns + "DEF", new object[] {
new XAttribute("rst","abc"),
123}),
new XElement(ns + "GHI", new object[] {
new XAttribute("uvw","def"),
456}),
new XElement(ns + "JKL", new object[] {
new XAttribute("xyz","ghi"),
789})
}));
doc.Save("filename");
}
}
}https://stackoverflow.com/questions/45571603
复制相似问题