繁体   English   中英

如何使用 XML/XElement 获取冒号:进入标签的第一部分...?

[英]How to use XML/XElement to get a colon : into the first part of the tag…?

我正在尝试创建一个 XML 文档。

我需要文件来显示这样的......

<HART:HART xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:HART="www.myurlhere.com" xsi:schemaLocation="www.myurlhere">

这是我正在使用的代码

        public FileStreamResult Submission(Models.Reporting.SubmissionViewModel svm)
    {
        MemoryStream ms = new MemoryStream();
        XmlWriterSettings xws = new XmlWriterSettings();

        xws.OmitXmlDeclaration = true;
        xws.Indent = true;

        XNamespace xmlns = XNamespace.Get("http://www.myurlhere.com");
        XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
        XNamespace schemaLocation = XNamespace.Get("http://www.myurlhere.com");

        using (XmlWriter xw = XmlWriter.Create(ms, xws))
        {
            XDocument doc = new XDocument(

                new XElement(xmlns + "HART",
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi),
                    new XAttribute(xsi + "schemaLocation", schemaLocation)


            ));
            doc.WriteTo(xw);
        }
        ms.Position = 0;
        return File(ms, "text/xml", "Sample.xml");
    }

但是它显示这样

<HART xmlns="http://www.myurlhere.com" xsi:schemaLocation="http://www.myurlhere.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

如您所见,第一部分只是 HART 而不是 HART:HART

我以前从未这样做过,因此将不胜感激任何指导或帮助。

谢谢

之前的部分:只是缩短命名空间的占位符。 这主要用于您需要使用多个命名空间。

通过在元素级别 als xmlns= 上指定命名空间,下面的所有孩子都将拥有这个命名空间,直到您再次指定一个带有 xmlns= 的孩子

如果您坚持使用<HART:HART xmlns:HART="">您可以这样做

new XElement(xmlns + "HART", new XAttribute(XNamespace.Xmlns + "HART", xmlns)          ));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM