阅读XML文件,将其更改并保存为新格式的XML
问题描述:
我有一个已从Orchard CMS导出的XML文件,我现在需要做的是将XML文件内的节点转换为新结构,以便我可以将文件导入Umbraco。阅读XML文件,将其更改并保存为新格式的XML
我该如何去做这件事?我想我可以编写一些c#.net来读取XML文件,然后进行我需要的更改,然后将它作为新文件。
的什么,我试图做的例子是:
导出的文件:
<BlogPost Id="/alias=The Blog\/2012\/09\/10\/on-starters-orders" Status="Published">
<TextField.Excerpt />
<TaxonomyField.Categories Terms="" />
<TaxonomyField.Tags Terms="" />
<BodyPart Text="MAIN CONTENT OF THE BLOG POST"
/>
<CommonPart Owner="/User.UserName=Owain" Container="/alias=blog" CreatedUtc="2012-09-10T13:27:00Z" PublishedUtc="2012-09-25T08:57:25Z" ModifiedUtc="2012-09-25T08:56:15Z" />
<AutoroutePart Alias="The Blog/2012/09/10/on-starters-orders" UseCustomPattern="false" />
<TitlePart Title="On starters orders....." />
<CommentsPart CommentsShown="true" CommentsActive="true" ThreadedComments="false" />
<TagsPart Tags="" />
</BlogPost>
我需要把它转换的是:
<posts>
<post id="1" date-created="2012-09-25T08:57:25Z" date-modified="2012-09-25T08:56:15Z" approved="true" post-url="on-starters-orders" type="normal" hasexcerpt="false" views="0" is-published="True">
<title type="text"><![CDATA[On starters orders.....]]></title>
<content type="text"><![CDATA[MAIN CONTENT OF THE BLOG POST]]>
</content>
<post-name type="text"><![CDATA[On starters orders.....]]></post-name>
<categories>
<category ref="1018" />
</categories>
<tags>
<tag ref="training" />
</tags>
<comments>
<comment id="35" date-created="2006-09-05T11:36:50" date-modified="2006-09-05T11:36:50" approved="false" user-name="Phil Haack" user-url="http://haacked.com">
<title type="text"><![CDATA[re: CS Dev Guide: Send Emails]]></title>
<content type="text"><![CDATA[Another test comment.]]></content>
</comment>
</comments>
<authors>
<author ref="Owain" />
</authors>
</post>
寻找的建议最好的办法做到这一点,因为我有150多个职位要转换,不喜欢手动做。
答
您可以使用XSLT转换,它有很多选项来执行您正在查找的内容。 XSLT可以用xml输出。
它绝对有可能做到。有很多xml读者。一个例子 - http://stackoverflow.com/questions/20005211/cannot-deserialize-xml-string-with-newtonsoft-json-jsonconvert-deserializeobject – 2015-04-02 21:11:13
使用['xsd.exe'](https://msdn.microsoft。 com/en-us/library/x6c1kb0s%28v = vs.110%29.aspx)为旧的和新的XML定义类,然后使用['XmlSerializer'](https://msdn.microsoft.com/en- us/library/system.xml.serialization.xmlserializer%28v = vs.110%29.aspx)加载旧的XML,编写c#代码以在两者之间进行映射,然后再次使用'XmlSerializer'来编写新的类。 – dbc 2015-04-02 21:13:22
或者,使用Linq-to-XML [直接在内存中修改您的XML](https://msdn.microsoft.com/zh-cn/library/bb387084.aspx)。 – dbc 2015-04-02 21:14:02