首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# XmlWriter WriteStartAttribute

C# XmlWriter WriteStartAttribute
EN

Stack Overflow用户
提问于 2017-08-08 14:52:09
回答 3查看 2.4K关注 0票数 1

我想将下面的标记写入XML文件中。

代码语言:javascript
复制
<StructureMetaData xsi:schemaLocation="MSD" xmlns="MSD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <!-- here a following more tags -->
</StructureMetaData>

对于这些,我做以下几点

代码语言:javascript
复制
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标记?

EN

回答 3

Stack Overflow用户

发布于 2017-08-08 19:46:13

你有几个问题:

  1. 在开始编写任何属性之前,您需要先编写根元素标记<StructureMetaData first。有一个默认的名称空间xmlns="MSD",因此您需要在该名称空间中编写它。
  2. 您正在调用XmlWriter.WriteStartAttribute(),但是此方法只写入属性名称。您仍然必须在名称之后编写,例如,使用XmlWriter.WriteString()。或者,您可以通过对XmlWriter.WriteAttributeString()的一次调用来编写名称和值。
  3. 您应该通过一个xmlWriter语句关闭并释放您的using

下面是使用WriteStartAttribute()的代码的工作版本

代码语言:javascript
复制
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()的更简单的版本

代码语言:javascript
复制
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小提琴显示这两个选项。

票数 2
EN

Stack Overflow用户

发布于 2017-08-08 14:57:54

您必须编写一个开始元素,在该元素上写入属性:

代码语言:javascript
复制
xmlWriter.WritestartElement("StructureMetaData");
xmlWriter.WriteStartAttribute(...);
票数 0
EN

Stack Overflow用户

发布于 2017-08-08 15:12:46

使用xml更容易:

代码语言:javascript
复制
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");
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45571603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档