向XmlAttribute添加xsd文件值

本文关键字:文件 xsd 添加 XmlAttribute | 更新日期: 2023-09-27 17:50:32

我如何使用下面的代码创建适当的xml字符串…

string myInputXmlString = @"<ApplicationData>
                                        <something>else</something>
                                    </ApplicationData>";
        var doc = new XmlDocument();
        doc.LoadXml(myInputXmlString);
        XmlAttribute newAttr = doc.CreateAttribute(
            "xsi", 
            "noNamespaceSchemaLocation", 
            "http://www.w3.org/2001/XMLSchema-instance");
        doc.DocumentElement.Attributes.Append(newAttr);
        var ms = new MemoryStream();
        XmlWriterSettings ws = new XmlWriterSettings
        {
            OmitXmlDeclaration = false,
            ConformanceLevel = ConformanceLevel.Document,
            Encoding = UTF8Encoding.UTF8
        };
        var tx = XmlWriter.Create(ms, ws);
        doc.Save(tx);
        tx.Flush();
        var xmlString = UTF8Encoding.UTF8.GetString(ms.ToArray());
        Console.WriteLine(xmlString);

我如何将xsd信息添加到此,使xml看起来像这样(与"FullModeDataset。xsd"萤火虫吗?

 <ApplicationData
  xsi:noNamespaceSchemaLocation="FullModeDataset.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

而不是当前代码输出的

 <ApplicationData
  xsi:noNamespaceSchemaLocation=""
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

向XmlAttribute添加xsd文件值

这是偶然的吗?

doc.DocumentElement.SetAttribute("noNamespaceSchemaLocation", 
        "http://www.w3.org/2001/XMLSchema-instance",
        "FullModeDataset.xsd");