麻烦与xsi:schemaLocation

时间:2016-02-04 16:07:48

标签: c# xml

我在这里和网络上的其他地方找到了关于这个主题的多个答案。但我无法找到在HTML中创建以下序列的解决方案:

XmlElement projectElement = rsil.CreateElement("RSIL", "Project", @"http://www.url.com/RSIL");
rsil.AppendChild(projectElement);
projectElement.SetAttribute("xmlns:xsi", @"http://www.w3.org/2001/XMLSchema-instance");
projectElement.SetAttribute("xmlns:eQ", @"http://www.url.com/eQ");
XmlAttribute grAt = rsil.CreateAttribute("xsi", "schemaLocation", "http://www.url.com/RSIL Name_RSIL.xsd");
projectElement.Attributes.Append(grAt);

无论如何,schemaLocation的xsi前缀丢失或者设置为d1p1。 根据结果​​,当前代码与此类似:

{{1}}

3 个答案:

答案 0 :(得分:1)

使用LINQ to XML,您可以使用

    XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance",
               RSIL = "http://www.url.com/RSIL",
               eQ = "http://www.url.com/eQ";

    XDocument doc = new XDocument(
        new XElement(RSIL + "Project",
                     new XAttribute(XNamespace.Xmlns + "RSIL", RSIL),
                     new XAttribute(XNamespace.Xmlns + "xsi", xsi),
                     new XAttribute(XNamespace.Xmlns + "eQ", eQ),
                     new XAttribute(xsi + "schemaLocation", "http://www.url.com/RSIL Name_RSIL.xsd")));

    Console.WriteLine(doc);

请参阅https://dotnetfiddle.net/iP2Gse

如果您需要在.NET框架中使用DOM实现,那么

    string xsi = "http://www.w3.org/2001/XMLSchema-instance",
               RSIL = "http://www.url.com/RSIL",
               eQ = "http://www.url.com/eQ";

    XmlDocument doc = new XmlDocument();

    XmlElement project = doc.CreateElement("RSIL:Project", RSIL);

    project.SetAttribute("xmlns:RSIL", RSIL);
    project.SetAttribute("xmlns:xsi", xsi);
    project.SetAttribute("xmlns:eQ", eQ);

    XmlAttribute schemaLoc = doc.CreateAttribute("xsi", "schemaLocation", xsi);
    schemaLoc.Value = "http://www.url.com/RSIL Name_RSIL.xsd";

    project.SetAttributeNode(schemaLoc);

    doc.AppendChild(project);


    Console.WriteLine(doc.OuterXml);

似乎可以胜任。

答案 1 :(得分:0)

使用XmlTextWriter可以这样做:

var writer = new XmlTextWriter("output.xml", Encoding.UTF8)
{
    Formatting = Formatting.Indented,
    Indentation = 1,
    IndentChar = '\t',
    Namespaces = true,
};

writer.WriteStartDocument();

writer.WriteStartElement("RSIL", "Project", "http://www.url.com/RSIL");
writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns:eQ", "http://www.url.com/eQ");
writer.WriteAttributeString("xsi:schemaLocation", "http://www.url.com/RSIL Name_RSIL.xsd");
writer.WriteEndElement();

writer.WriteEndDocument();

writer.Flush();
writer.Close();

这导致至少几乎完全符合您的要求:

<RSIL:Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:eQ="http://www.url.com/eQ" xsi:schemaLocation="http://www.url.com/RSIL Name_RSIL.xsd" xmlns:RSIL="http://www.url.com/RSIL" />

答案 2 :(得分:0)

查看本文

https://social.msdn.microsoft.com/Forums/en-US/fa7205f7-f5f5-40bb-9b14-cb64c1ba5665/schemalocation-problem-with-serialization?forum=xmlandnetfx


通常,schemaLocation属性属于命名空间http://www.w3.org/2001/XMLSchema-instance,并具有前缀xsi。您可以实现以下目标:

代码段

public class exampleClass

{

public exampleClass() { }

[System.Xml.Serialization.XmlAttributeAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]

public string schemaLocation = "http://www.rewerse.net/I1/2006/R2ML http://oxygen.informatik.tu-cottbus.de/R2ML/0.4/R2ML.xsd";

}



XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

exampleClass example = new exampleClass();

XmlSerializer serializer = new XmlSerializer(typeof(exampleClass));

serializer.Serialize(Console.Out, example, namespaces);