【问题标题】:Creating an XML file using Schema使用 Schema 创建 XML 文件
【发布时间】:2012-03-28 08:20:33
【问题描述】:

(与我的问题XML Parsing using a schema in C#有关)

我正在为一个应用程序编写一些代码,该应用程序将使用 XML 来存储业务对象(它们被设计得非常小,所以我认为 XML 可能比使用文本更容易解析、处理和检查文件或一些自定义文件格式)。我已经编写了架构,并且想知道如何在输出的 XML 文件中添加对此架构的引用。

与我链接的问题一样,我正在使用 XmlDocument 编写文件,因为我必须在 .net 3.5 中工作 - 与我链接的问题相同的原因。我还使用 XmlDeclaration、XmlNode 和 XmlElement 将 xml 文件的各个部分写入 XmlDocument 对象。

这是我正在做的一个例子:

if (!File.Exists(fileName))
{
    XmlDocument xmlDocumentWriter = new XmlDocument();
    XmlDeclaration xmlDec = xmlDocumentWriter.CreateXmlDeclaration("1.0", "utf-8", null);
    xmlDocumentWriter.AppendChild(xmlDec);

    XmlNode root = xmlDocumentWriter.CreateElement("root");
    XmlElement childNodeInfo = xmlDocumentWriter.CreateElement("parent-of-some-child");
    root.AppendChild(childNodeInfo);

    childNodeInfor.AppendChild(xmlDocumentWriter.CreateElement("SomeChild")).InnerText = _somevariable.ToString();
    /* continue in this way until they've all been added */

    xmlDocumentWriter.Appendchild(root);
    xmlDocumentWriter.Save(fileName);

    /* close all open streams and xml objects */
}

这将创建一个像这样的 xml 文件:

<?xml version="1.0" encoding="utf-8" ?>
    <root>
        <parent-of-some-child>
        <somechild>value</somechild>
        </parent-of-some-child>
    </root>

但是,我将如何向根节点添加架构引用?

我编写的架构类似于:

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns="urn:schema-name"
  elementFormDefault="qualified">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="parent-of-some-child">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="some-child" type="xsd:string"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element name="parent-of-some-child">
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element name="root">

我正在寻找这样的东西:

<?xml version="1.0" encoding="utf-8" ?>
    <root xmlns="reference-to-schema">
        <parent-of-some-child>
        <somechild>value</somechild>
        </parent-of-some-child>
    </root>

或者我什至需要对 xml 文件中的架构的引用?在打开和解析过程中使用模式检查xml就足够了吗?

【问题讨论】:

标签: xml visual-studio-2008 c#-3.0 xsd


【解决方案1】:

这是一个例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace XmlNameSpace
{
    class Program
    {
        static void Main(string[] args)
        {
            XNamespace xmlns = XNamespace.Get("urn:schema-name");
            XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
            XNamespace schemaLocation = XNamespace.Get("urn:schema-name schema.xsd");

            XDocument p =  new XDocument(
                new XElement(xmlns + "root",
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi),
                    new XAttribute(xsi + "schemaLocation", schemaLocation),
                    new XElement("parent-of-some-child",
                        new XElement("somechild","value"))
                )
            );


            p.Save("c:\\test.xml");


        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-07-29
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多