XmlReader - 我需要编辑一个元素并生成一个新元素 [英] XmlReader - I need to edit an element and produce a new one

查看:13
本文介绍了XmlReader - 我需要编辑一个元素并生成一个新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在覆盖一个传入 XmlReader 的方法,我需要找到一个特定的元素,添加一个属性,然后创建一个新的 XmlReader 或仅用修改后的内容替换现有的 XmlReader.我使用的是 C#4.0

I am overriding a method which has an XmlReader being passed in, I need to find a specific element, add an attribute and then either create a new XmlReader or just replace the existing one with the modified content. I am using C#4.0

我已经使用 XElement (Linq) 进行了调查,但我似乎无法操作现有元素并添加属性和值.

I have investigated using XElement (Linq) but I can't seem to manipulate an existing element and add an attribute and value.

我知道 XmlWriter 有 WriteAttributeString 这会很棒,但我又不确定它是如何组合在一起的

I know that the XmlWriter has WriteAttributeString which would be fantastic but again I am not sure how it all fits together

我希望能够做类似的事情---这是伪代码!

I would like to be able to do something like --- This is pseudo-code!

public XmlReader DoSomethingWonderful(XmlReader reader)
{
   Element element = reader.GetElement("Test");
   element.SetAttribute("TestAttribute","This is a test");
   reader.UpdateElement(element);
   return reader;
}

推荐答案

XmlReader/Writer 是顺序访问流.您必须在一端读入,按照您想要的方式处理流,然后在另一端写出.优点是您不需要将整个内容读入内存并构建 DOM,这是您使用任何基于 XmlDocument 的方法所获得的.

XmlReader/Writer are sequential access streams. You will have to read in on one end, process the stream how you want, and write it out the other end. The advantage is that you don't need to read the whole thing into memory and build a DOM, which is what you'd get with any XmlDocument-based approach.

这个方法应该会让你开始:

This method should get you started:

private static void PostProcess(Stream inStream, Stream outStream)
{
    var settings = new XmlWriterSettings() { Indent = true, IndentChars = " " };

    using (var reader = XmlReader.Create(inStream))
    using (var writer = XmlWriter.Create(outStream, settings)) {
        while (reader.Read()) {
            switch (reader.NodeType) {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Prefix, reader.Name, reader.NamespaceURI);
                    writer.WriteAttributes(reader, true);

                    //
                    // check if this is the node you want, inject attributes here.
                    //

                    if (reader.IsEmptyElement) {
                        writer.WriteEndElement();
                    }
                    break;

                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    break;

                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;

                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;

                case XmlNodeType.SignificantWhitespace:
                    writer.WriteWhitespace(reader.Value);
                    break;
            }
        }
    }
}

这不像派生自己的 XmlWriter 那样干净,但我发现它更容易.

This is not quite as clean as deriving your own XmlWriter, but I find that it's much easier.

如何同时打开两个流的示例可能如下所示:

An example of how you would open two streams at once might be something like this:

using (FileStream readStream = new FileStream(@"c:myFile.xml", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Write)) {
  using (FileStream writeStream = new FileStream(@"c:myFile.xml", FileMode.OpenOrCreate, FileAccess.Write)) {
    PostProcess(readStream, writeStream);
  }
}

这篇关于XmlReader - 我需要编辑一个元素并生成一个新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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