简体   繁体   English

在根标记中具有多个名称空间的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 ")] 如果我只有一个名称空间,则可以通过在类[XmlRoot(ElementName =“ metadata”,Namespace =“ http://medical.nema.org/mint ”)]中包含该名称空间来反序列化

How to deserialize if i have multiple namespaces in XML ? 如果我在XML中有多个名称空间,如何反序列化? How should my class look like ? 我的班级应该怎么样?

Part of XML file 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 where i am deserializing 我正在反序列化的控制器类

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

You need to add the namespace property to your class definition. 您需要将namespace属性添加到类定义中。

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

Also, if you have access to the full xsd or even xml file you can use the xsd.exe tool which ships as part of Visual Studio to generate the required classes. 另外,如果您有权访问完整的xsd或xml文件,则可以使用xsd.exe工具(该工具作为Visual Studio的一部分提供)来生成所需的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM