XML 的反序列化在根标记中具有多个命名空间

Deserialization of XML having multiple namespace in root tag

If i just have a single namespace i am able to deserialize by including the namespace as this in class [XmlRoot(ElementName = "metadata", Namespace = "http://medical.nema.org/mint")]

How to deserialize if i have multiple namespaces in XML ? How should my class look like ?

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">

Class

[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;}
}

Controller class 我反序列化的地方

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

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

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

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