Requirement is to add CData section to existing XML string of below format
Required output is to be like this
Basically without CData section when i try to load xml string into XDocument, .net is giving hex decimal parsing error.
Hoping adding CData section will solve the problem.
Awaiting for reply
Thanks in advance
Amit ChoudharyPosted Jan 20, 2011, 11:59 PM
Below code will show you how to add CData section in xml file:
XmlDocument xmldoc = new XmlDocument();
XmlNode xmlRoot, xmlNode, xmlitem;
xmlRoot = xmldoc.CreateElement("Item");
xmldoc.AppendChild(xmlRoot);
xmlitem = xmldoc.CreateElement("item");
xmlRoot.AppendChild(xmlitem);
xmlNode = xmldoc.CreateElement("name");
xmlitem.AppendChild(xmlNode);
XmlCDataSection cdata = xmldoc.CreateCDataSection(getFileName(file));
xmlNode.AppendChild(cdata);
In you case you can find the XmlNode by other techniques like e.g., below is the code how to navigate to particular tag in existing XML
XmlTextReader reader = new XmlTextReader(project);
XmlDocument m_document = new XmlDocument();
m_document.Load(reader);
//Instantiate an XmlNamespaceManager object.
System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(m_document.NameTable);
//Add the namespaces used in books.xml to the XmlNamespaceManager.
xmlnsManager.AddNamespace("ms", "http://schemas.microsoft.com/developer/msbuild/2003");
reader.Close();
System.Xml.XmlNodeList nl;
XmlNode node = m_document.DocumentElement;
XmlNodeList nList = node.SelectNodes("/ms:Project/ms:ItemDefinitionGroup",xmlnsManager);
Hope this helps you.
Thanks