最好的方式来读取,修改,和编写XML [英] Best way to read, modify, and write XML

查看:145
本文介绍了最好的方式来读取,修改,和编写XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计划是用我的C#程序中的XML文档中读取,搜索我想要更改的特定条目,然后写出修改后的文档。不过,我已经成为未粘住的,因为它很难元素之间的区分,他们是否开始或使用我使用的文件中读取其中XmlTextReader的结束。我可以做一些建议把我在正确的轨道上。



该文件是一个HTML文件,所以你可以想像,这是相当复杂的。



我喜欢搜索HTML文档中的元素的ID,因此,例如寻找这个和更改SRC会;

 < IMG的边界=0SRC =bigpicture.pngWIDTH =248HEIGHT =36ALT =ID =lookforthis/> 


解决方案

如果它实际上是有效的XML,而且很容易适应记忆,我会选择 LINQ到XML 的XDocument 的XElement 等)每次。这是迄今为止最好的XML API我用。这很容易形成查询,并容易构造新的元素了。



您可以使用XPath在哪里这是适当的,或内置轴方法(元素()后裔()属性()等)。如果你可以让我们知道您遇到很难有什么具体的位,我会很乐意帮助解决如何表达他们在LINQ to XML。



如果,另一方面,这是HTML它的不是的有效的XML,你就会有一个更难的时间 - 因为XML的API generalyl期待与有效的XML文档工作。你可以使用 HTMLTidy 第一疗程,但是的可以的有不良的影响。



有关您的具体例子:

 的XDocument DOC = XDocument.Load(file.xml) ;如果属性丢失
的字符串src =(字符串)IMG
的foreach(在doc.Descendants VAR IMG(IMG))
{
// src是空。属性(SRC);
img.SetAttributeValue(src用户,SRC +与-变化);
}


My plan is to read in an XML document using my C# program, search for particular entries which I'd like to change, and then write out the modified document. However, I've become unstuck because it's hard to differentiate between elements, whether they start or end using XmlTextReader which I'm using to read in the file. I could do with a bit of advice to put me on the right track.

The document is a HTML document, so as you can imagine, it's quite complicated.

I'd like to search for an element id within the HTML document, so for example look for this and change the src;

<img border="0" src="bigpicture.png" width="248" height="36" alt="" id="lookforthis" />

解决方案

If it's actually valid XML, and will easily fit in memory, I'd choose LINQ to XML (XDocument, XElement etc) every time. It's by far the nicest XML API I've used. It's easy to form queries, and easy to construct new elements too.

You can use XPath where that's appropriate, or the built-in axis methods (Elements(), Descendants(), Attributes() etc). If you could let us know what specific bits you're having a hard time with, I'd be happy to help work out how to express them in LINQ to XML.

If, on the other hand, this is HTML which isn't valid XML, you'll have a much harder time - because XML APIs generalyl expect to work with valid XML documents. You could use HTMLTidy first of course, but that may have undesirable effects.

For your specific example:

XDocument doc = XDocument.Load("file.xml");
foreach (var img in doc.Descendants("img"))
{
    // src will be null if the attribute is missing
    string src = (string) img.Attribute("src");
    img.SetAttributeValue("src", src + "with-changes");
}

这篇关于最好的方式来读取,修改,和编写XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆