简体   繁体   English

使用C#遍历父子组合xml

[英]looping through parent child combination xml using c#

I am new to xml, i want to looping through the below xml and save the records in the database. 我是xml的新手,我想遍历以下xml并将记录保存在数据库中。 the below xml is parent child combination, parts as to be saved under respective parents. 下面的xml是父级子级组合,要保存在相应父级下的部分。 if the source field is M then there will be child item for that. 如果源字段为M,则将有子项。

  <PARENT>
 <COMPONENT>
  <part>test1</part><source>M</source><ParentPart>parent</ParentPart>
  <SUBCOMPONENT>
   <part>test2</part><source>B</source><ParentPart>test1</ParentPart>
  </SUBCOMPONENT>

  <SUBCOMPONENT>
   <part>test3</part><source>M</source><ParentPart>test1</ParentPart>
   <SUBSUBCOMPONENT>
    <part>test4</part><source>B</source><ParentPart>test3</ParentPart>
   </SUBSUBCOMPONENT>
  </SUBCOMPONENT>

 </COMPONENT>

 <COMPONENT>
  <part>part1</part><source>B</source><ParentPart>parent</ParentPart>
 </COMPONENT>

 <COMPONENT>
   <part>part2</part><source>M</source><ParentPart>parent</ParentPart>
  <SUBCOMPONENT>
   <part>part3</part><source>B</source><ParentPart>part2</ParentPart>
  </SUBCOMPONENT>

  <SUBCOMPONENT>
   <part>part4</part><source>M</source><ParentPart>part2</ParentPart>
   <SUBSUBCOMPONENT>
    <part>part5</part><source>B</source><ParentPart>part4</ParentPart>
   </SUBSUBCOMPONENT>
  </SUBCOMPONENT>

 </COMPONENT>

</PARENT>

any help would be appropriated. 任何帮助将是适当的。

Thanks, 谢谢,

Pradeep 普拉迪普

You have a lot of options here: - if you have xml schema, use xsd.exe which generates classes you fillup with XmlSerializer. 您在这里有很多选择:-如果您具有xml架构,请使用xsd.exe生成使用XmlSerializer填充的类。 - XmlDocument allows you to do XPath queries. -XmlDocument允许您执行XPath查询。 - consider XmlTextReader if your files are large and you need fast proccesing. -如果文件很大并且需要快速处理,请考虑使用XmlTextReader。

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

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