如何修改从XmlWriter生成的XML Stream

时间:2015-03-20 12:52:21

标签: c# xmldocument xmlwriter

我需要使用XmlWriter生成的XML并对其进行修改。有可能吗?

作为“编写者”的XmlWriter,“writeStream”是一个IOStream,“value”是一个项目列表,这里是用XmlWriter编写的代码:

writer.WriteRaw("<array>");                      
                    writer.WriteRaw("<curLength>" + ((IList)value).Count + "</curLength>");
                    writer.Close();

                    serializer = new XmlSerializer(type);


                    XmlWriter loopWriter;
                    foreach (object o in (IList)value)
                    {
                        loopWriter = XmlWriter.Create(writeStream, new XmlWriterSettings { OmitXmlDeclaration = true });
                        serializer = new XmlSerializer(o.GetType());

                        serializer.Serialize(loopWriter, o);
                        loopWriter.Close();
                    }                        

                    writer = CreateXmlWriter(writeStream, content);
                    writer.WriteRaw("</array>");                        
                    writer.Close();

之后我想获取XmlWriter生成的流并修改它,做这样的事情:

XmlDocument doc = new XmlDocument();
                    doc.Load(writeStream);

                    var elements = doc.GetElementsByTagName("struct");
                    int numberOfElements = elements.Count;
                    for(int i=1; i<=numberOfElements; i++)
                    {                            
                        XmlNode element = elements.Item(i);
                        XmlElement indexElement = doc.CreateElement("index");
                        indexElement.InnerText = i.ToString();
                        element.InsertAfter(indexElement,element);
                    }

但是,如果我执行代码,它会在“doc.Load(writeStream);”行引发“NotSupportedException”。

谢谢!

0 个答案:

没有答案