How to modify DocumentElement.Prefix in XmlDocument

pumpentester

New Member
I have some sets of xml in which I have to add namespace prefix on nodes which doesn't have any prefix.I have written a code which works for all nodes other than root element.Please point me to the thing which I can do change the root element prefix as well.\[code\] private void ReplaceFile(string xmlfile) { XmlDocument doc = new XmlDocument(); doc.Load(xmlfile); var a = doc.CreateAttribute("xmlns:mailxml12tm"); a.Value = "http://idealliance.org/Specs/mailxml12.0a/mailxml_tm"; doc.DocumentElement.Attributes.Append(a); doc.DocumentElement.Prefix = "mailxml12tm"; //foreach (XmlNode item in doc.SelectNodes("//*").Cast<XmlNode>().Where(item => item.Prefix.Length == 0)) //{ // item.Prefix = "mailxml12tm"; //} doc.Save(xmlfile);}\[/code\]The xml file:\[code\]<DeliveryApptCreateRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" p3:ApptType="Pallet" p3:PickupOrDelivery="Delivery" p3:ShipperApptRequestID="4490B0C07355" p3:SchedulerCRID="6498874" xmlns:p3="http://idealliance.org/Specs/mailxml12.0a/mailxml_defs"> <SubmitterTrackingID xmlns="http://idealliance.org/Specs/mailxml12.0a/mailxml_tm">2CAD3FBC71B1E1517021</SubmitterTrackingID> <DestinationEntry xmlns="http://idealliance.org/Specs/mailxml12.0a/mailxml_tm">No</DestinationEntry> <OneTimeAppt xmlns="http://idealliance.org/Specs/mailxml12.0a/mailxml_tm"> <PreferredAppt>2012-07-01T09:00:00Z</PreferredAppt> </OneTimeAppt> </DeliveryApptCreateRequest>\[/code\]
 
Back
Top