【问题标题】:Deserialization of XML having multiple namespace in root tag在根标记中具有多个命名空间的 XML 的反序列化
【发布时间】:2017-06-21 05:21:17
【问题描述】:

如果我只有一个命名空间,我可以反序列化 在类 [XmlRoot(ElementName = "metadata", 命名空间 = "http://medical.nema.org/mint")]

如果我在 XML 中有多个命名空间,如何反序列化?我的班级应该是什么样子?

部分 XML 文件

<metadata type="DICOM" version="1.0" xmlns="http://medical.nema.org/mint"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://medical.nema.org/mint http://medical-imaging-network-transport.googlecode.com/files/mint-datadictionary.xsd">

[Serializable]
    [XmlRoot(ElementName = "metadata")]

    public class Dicom
    {
        [XmlAttribute]
        public string type { get; set; }

        [XmlAttribute]
        public string version { get; set; }

        [XmlElement("attributes")]
        public List<attributes> attributes { get; set; }

        [XmlElement("study-attributes")]
        public List<studyattributes> studyattributes {get; set;}

        [XmlElement("series-attributes")]
        public List<seriesattributes> seriesattributes{get;set;}
}

我要反序列化的控制器类

XmlSerializer deserializer = new XmlSerializer(typeof(Dicom));
        TextReader reader = new StreamReader(@"F:\DICOM.xml");
  object obj = deserializer.Deserialize(reader);

【问题讨论】:

    标签: c# xml asp.net-web-api2 xml-serialization xml-deserialization


    【解决方案1】:

    您需要将命名空间属性添加到您的类定义中。

    [XmlRoot(ElementName = "metadata", Namespace = "http://medical.nema.org/mint")]
    

    此外,如果您可以访问完整的 xsd 甚至 xml 文件,则可以使用作为 Visual Studio 一部分提供的 xsd.exe 工具来生成所需的类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多