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

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

问题描述

我重写它有一个XmlReader传递的方法,我需要找到一个特定元素,添加属性,然后创建一个新的XmlReader或仅取代现有的与修改的内容。我使用C#4.0



使用的XElement(LINQ),但我似乎无法操作现有元素,并添加属性和价值我已经调查。



我知道的XmlWriter具有WriteAttributeString这将是非常美妙的,但我又是不知道这一切是如何结合在一起



我希望能够做这样的事---这是伪代码!

 公开的XmlReader DoSomethingWonderful(XmlReader中读取)
{
元素的元素= reader.GetElement(测试);
element.SetAttribute(TestAttribute,这是一个测试);
reader.UpdateElement(元);
返回阅读器;
}


解决方案

的XmlReader /写卡器是顺序存取流。您必须阅读一端,处理你想怎么流,并写入另一端。它的优点是,你并不需要阅读整个事情到内存中,并生成一个DOM,这是你与任何基于的XmlDocument的方法得到的。



这方法应该让你开始:

 私有静态无效后处理(流inStream,流outStream)
{
VAR设置=新XmlWriterSettings(){缩进= TRUE,IndentChars =};使用

(VAR读卡器= XmlReader.Create(插播广告))使用
(VAR作家= XmlWriter.Create(outStream,设置)){
,而(reader.Read() ){
开关(reader.NodeType){
情况下XmlNodeType.Element:
writer.WriteStartElement(reader.Prefix,reader.Name,reader.NamespaceURI);
writer.WriteAttributes(读卡器,真); //检查

//
如果这是你想要的节点,这里注入的属性。
//

如果(reader.IsEmptyElement){
writer.WriteEndElement();
}
中断;

情况下XmlNodeType.Text:
writer.WriteString(reader.Value);
中断;

情况下XmlNodeType.EndElement:
writer.WriteFullEndElement();
中断;

情况下XmlNodeType.XmlDeclaration:
情况下XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name,reader.Value);
中断;

情况下XmlNodeType.SignificantWhitespace:
writer.WriteWhitespace(reader.Value);
中断;
}
}
}
}

这是不是很派生自己的XmlWriter一样干净,但我发现它更容易。





的你将如何打开两个流一次可能是这样一个例子:

 使用(的FileStream readStream =新的FileStream(@C:\myFile.xml,FileMode.OpenOrCreate,FileAccess.Read,FileShare.Write)){使用(的FileStream writeStream =新的FileStream(@C 
:\\ \\myFile.xml,FileMode.OpenOrCreate,FileAccess.Write)){
后处理(readStream,writeStream);
}
}


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

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

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 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;
            }
        }
    }
}

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

[EDIT]

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天全站免登陆