【问题标题】:XSD Generated Class containing a System.Xml.XmlElement property包含 System.Xml.XmlElement 属性的 XSD 生成的类
【发布时间】:2018-10-30 11:02:30
【问题描述】:

我目前正在生成用于配置服务器的 XML 文件。

我有一个使用 xsd 生成的类,其中包含 System.Xml.XmlElement 类型的属性。

public class GeneratedClass
{
    ...

    private System.Xml.XmlElement informationField;

    [System.Xml.Serialization.XmlArrayItemAttribute("Information", IsNullable=false)]
    public System.Xml.XmlElement Information {
        get {
            return this.informationField;
        }
        set {
            this.informationField = value;
        }
    }

    ...
}

我在将自定义对象“注入”到此信息属性时遇到了麻烦。

public class MyExampleObject
{
    public string Name { get; set; }
    public string Id { get; set;
}

程序反序列化一个GeneratedClass 类型的xml 文件,然后我想将MyExampleObject 添加到Informations 属性中。

我目前的做法是用这个方法:

        XmlDocument doc = new XmlDocument();

        using (XmlWriter writer = doc.CreateNavigator().AppendChild())
        {
            XmlSerializer serializer = new XmlSerializer(typeof(MyExampleObject));
            serializer.Serialize(writer, MyObject);
        }

        this.Information = doc.DocumentElement;

在此之后,我将整个对象序列化为文件,但是当我这样做时,我得到了不需要的 xml 命名空间属性。 <Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="">

我发现其他有类似问题的帖子,但建议的解决方案给我留下了<Information xmlns="">,这仍然不是一个完整的解决方案。

我觉得可能有其他方法可以做到这一点,但我不确定如何。

有什么建议吗?

【问题讨论】:

    标签: c# xml xsd


    【解决方案1】:

    我知道这篇文章已经很老了 :) 但也许对于其他寻求答案的人来说可能会派上用场

    更新抱歉,我有点快速阅读了您的问题,并专注于我自己的问题,这在某种意义上也与命名空间有关,但更多的是如何删除一个。我看到您使用XmlSerializer,不幸的是我不知道如何操作作为属性添加到类上的命名空间以进行序列化。也许看看XmlRootAttribute,你可以传递给XmlSerializer.ctor,然后你有XmlSerializerNamespaces,它在序列化期间传递并提供操作命名空间的手段。

    我最近也偶然发现了这个问题并找到了解决方案。

    var xmlDocument = new XmlDocument();
    var xmlElement = xmlDocument.CreateElement(Prefix, "Document", Ns);
    xmlElement.InnerXml = string.Empty;
    var navigator = xmlElement.CreateNavigator();
    navigator.AppendChildElement(Prefix, "InfRspnSD1", Ns, null);
    navigator.MoveToFirstChild();
    navigator.AppendChildElement(Prefix, "InvstgtnId", Ns, "123456789");
    // At this point xmlElement contains XML and can be assigned
    // to the generated class
    
    // For pretty print only
    using var sw = new StringWriter();
    using var writer = System.Xml.XmlWriter.Create(sw, Settings);
    xmlElement.WriteTo(writer);
    writer.Flush();
    
    return sw.ToString();
    

    (Code on GitHub)

    我注意到命名空间被包含了两次,一次在根节点中,然后再次是直接后代(第一个子节点)。

    # Generate XML using XNavigator
    <?xml version="1.0" encoding="utf-16"?>
    <supl:Document xmlns:supl="urn:iso:std:iso:20022:tech:xsd:supl.027.001.01">
       <supl:InfRspnSD1 xmlns:supl="urn:iso:std:iso:20022:tech:xsd:supl.027.001.01">
         <supl:InvstgtnId>123456789</supl:InvstgtnId>
       </supl:InfRspnSD1>
    </supl:Document>
    --------------------
    

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 2012-01-27
      • 2020-05-22
      相关资源
      最近更新 更多