Need help with xml editing using xmltextwriter
-
21. června 2012 11:49Hi,
I have an xml file with the below structure:
<Root>
<Node1>Node1Data</Node1>
<Node2>Node2Data</Node2>
</Root>
The code has been implemented using XmlTextWriter in the base class.
My requirement is to use the overridden method in the child class to open the xml and append some more nodes to get the below structure:
<Root>
<Node1>Node1Data</Node1>
<Node2>Node2Data</Node2>
<Node3>Node3Data</Node3>
</Root>
Using a StreamWriter in the XmlTextWriter constructor, I am able to write the Node3 after the ending Root node and not inside. Kindly help.
Regards,
Asim.
Všechny reakce
-
22. června 2012 8:56Moderátor
Hi Asim Patnaik,
Welcome to MSDN Forum.
I wrote a demo for you. Please refer to it.
Original XML file:
<?xml version="1.0" encoding="utf-8"?> <Person> <Name>Allen</Name> <Age>24</Age> </Person>
C# code:
static void Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("testXML.xml"); XmlNode root = xmlDoc.DocumentElement; foreach (XmlNode node in root.ChildNodes) { if (node.Name == "Age") { XmlNode newNode = xmlDoc.CreateElement("Sex"); newNode.InnerText = "Male"; root.InsertAfter(newNode, node); } } xmlDoc.Save("testXML.xml"); }The final XML file:
<?xml version="1.0" encoding="utf-8"?> <Person> <Name>Allen</Name> <Age>24</Age> <Sex>Male</Sex> </Person>
Best RegardsAllen Li [MSFT]
MSDN Community Support | Feedback to us
- Navržen jako odpověď LimitedDeadFat 27. června 2012 13:15
-
25. června 2012 2:38Moderátor
Hi Asim Patnaik,
Any update about this issue? If you need further help, please feel free to let me know.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
-
26. června 2012 13:08
Hi,
The above provided solution is using XmlDocument. As mentioned in my first post, I have to achieve that using XmlTextWriter as I cannot change the base class implementation.
Regards,
Asim.
-
27. června 2012 8:32
Why can not you use the XML document when overriding the base class method? Can you show what is the signature of the method you are overriding?
-
27. června 2012 9:02Moderátor
Hi Asim Patnaik,
The introduction about XMLTextWriter on MSDN as below:
"Represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations."
It is forward-only, so it couldn't be used to modify the file, if XMLTextWriter is necessary, the only way is to write whole xml.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
- Označen jako odpověď Allen Li - AI3Microsoft Contingent Staff, Moderator 1. července 2012 9:30
-
27. června 2012 15:23
Thanks for your responses.
Actually, the implementation is that an instance of the XmlTextWriter is passed across methods and the methods keep adding the data to the writer. So we cannot use XmlDocument.
Due to this bottleneck, we are suggesting a design change :) Hope it comes through.
Regards,
Asim.
-
28. června 2012 2:17Moderátor
Hi Asim Patnaik,
Any update about this issue? If you need futher help, please feel free to let me know. I will be more than happy to be of assistance.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us