2017-09-26 90 views
0

我试图通过序列化我的对象到XML构建CDA文件已经存在,这里的XML部分是害我一些麻烦:XML元素的命名空间“TR”“”是在当前范围内

<component> 
    <section> 
     <templateId root='2.16.840.1.113883.10.20.1.11'/> 
     <templateId root='1.3.6.1.4.1.19376.1.5.3.1.3.6'/> 
     <!--<id root='' extension=''/>--> 
     <code code="11450-4" displayName="PROBLEM LIST" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC"/> 
     <title>Active Problem - Problem List</title> 
     <text> 
      <table> 
       <thead> 
        <tr> 
         <th>Problem</th> 
         <th>Code</th> 
         <th>Code System</th> 
         <th>Start Date</th> 
         <th>Status</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
         <td>Asthma</td> 
         <td>195967001</td> 
         <td>SNOMED CT</td> 
         <td></td> 
         <td>Active</td> 
        </tr> 
        <tr> 
         <td>Costal chondritis</td> 
         <td>64109004</td> 
         <td>SNOMED CT</td> 
         <td></td> 
         <td>Active</td> 
        </tr> 
        <tr> 
         <td>No impairment</td> 
         <td>66557003</td> 
         <td>SNOMED CT</td> 
         <td></td> 
         <td>Active</td> 
        </tr> 
       </tbody> 
      </table> 
     </text> 
    </section> 
</component> 

这里是我的C#类其序列:

public class Section 
{ 
    [XmlElement("templateId")] 
    public List<IdElement> TemplateIds { get; set; } 
    [XmlElement("code")] 
    public CodeElement Code { get; set; } 
    [XmlElement("title")] 
    public string Title { get; set; } 
    [XmlElement("text")] 
    public Text Text { get; set; } 
} 
public class Text 
{ 
    [XmlElement("table")] 
    public Table.Table Table { get; set; } 

    [XmlArray("list")] 
    [XmlArrayItem("item")] 
    public List<string> List { get; set; } 

    [XmlElement("paragraph")] 
    public List<string> Paragraphs { get; set; } 
} 
public class Table 
{ 
    [XmlElement("thead")] 
    public TRow Header { get; set; } 
    [XmlElement("tbody")] 
    public TRow Body { get; set; } 
} 
public class TRow 
{ 
    [XmlArray(ElementName = "tr", Namespace = "")] 
    [XmlArrayItem("td")] 
    public List<string> RowData { get; set; } 

    [XmlArray(ElementName = "tr", Namespace = "")] 
    [XmlArrayItem("th")] 
    public List<string> HeaderData { get; set; } 
} 

但是当我尝试现在连载我的CDA对象,它说,该型TR是在命名空间已经存在,所以我想这这种XML表已经存在,但我可以'找到一个正确的做法。 有没有解决方法来解决这个问题?

这里的错误日志(没有堆栈跟踪):

System.InvalidOperationException:有反射类型 'Project.Cda.Core.ClinicalDocument' 错误。 ---> System.InvalidOperationException:有一个错误反映了 属性'组件'。 ---> System.InvalidOperationException:有 反映类型'Project.Cda.Core.Components.BaseComponent'的错误。 ---> System.InvalidOperationException:反映属性'组件'时出错。 ---> System.InvalidOperationException:有 是反映类型'Project.Cda.Core.Components.Component'的错误。 ---> System.InvalidOperationException:反映属性'Section'时发生错误。 ---> System.InvalidOperationException:有 反映类型'Project.Cda.Core.Components.Section'的错误。 ---> System.InvalidOperationException:有一个错误反映了 属性'Text'。 ---> System.InvalidOperationException:有一个反映类型'Project.Cda.Core.Components.Text'的错误 。 ---> System.InvalidOperationException:反映 属性“表”的错误。 ---> System.InvalidOperationException:有一个反映类型为“Project.Cda.Core.Components.Table.Table”的 错误。 ---> System.InvalidOperationException:有一个错误反映了 属性'标题'。 ---> System.InvalidOperationException:有一个反映类型'Project.Cda.Core.Components.Table.TRow'的错误 。 ---> System.InvalidOperationException:有一个错误反映了 属性的'HeaderData'。 ---> System.InvalidOperationException:XML 元素'tr'来自命名空间''已经存在于当前的 范围内。使用XML属性为元素指定另一个XML名称或命名空间。

+2

在'TRow'你在'RowData'和'HeaderData'上都有'[XmlArray(ElementName =“tr”,Namespace =“”)]''。这是行不通的。 – dbc

+0

@dbc嗯,谢谢,我会尝试别的。 是我唯一的选择为标题和正文数据创建单独的类吗? –

+2

这是迄今为止最简单的。其他选项包括1)使用['[XmlChoiceIdentifierAttribute]'](https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlchoiceidentifierattribute(v = vs.110).aspx)设置指示''子元素是'​​'还是''的'enum'数组。 2)实现['IXmlSerializable'](https://www.codeproject.com/Articles/43237/How-to-Implement-IXmlSerializable-Correctly)。 (不要这样做!)3)如图所示,在'[XmlAnyElement]'属性中嵌套序列化。 [这里](https://stackoverflow.com/a/33180709/3744182)。 – dbc

回答

2

,因为在TRow,你在两个RowDataHeaderData

[XmlArray(ElementName = "tr", Namespace = "")] 

你所得到的错误。这是行不通的 - 你试图为两个不同的属性指定相同的元素名称,因此得到你看到的错误,即命名空间的XML元素'tr'已经存在于当前作用域中。

此外,您的模型还有一个额外的问题。重复<tbody>中的<tr>元素,但您的数据模型只允许每个物体有一个<tr>元素。

以下修复都通过引入中间TablePart问题来表示任一表中的标题和正文部分:

public class Table 
{ 
    [XmlElement("thead")] 
    public TablePart Header { get; set; } 

    [XmlElement("tbody")] 
    public TablePart Body { get; set; } 
} 

public class TablePart 
{ 
    [XmlElement(ElementName = "tr", Namespace = "")] 
    public List<TableRow> RowData { get; set; } 
} 

public class TableRow 
{ 
    [XmlElement("td")] 
    public List<string> Data { get; set; } 

    [XmlElement("th")] 
    public List<string> Headers { get; set; } 
} 

样品fiddle