Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an XML file and I need to insert xmlns="my namespace url" into the root element.

Also I need to insert xmlns="" into all elements.

How can I do that?
Also it is possible to do this (add xmlns="" into all elements) without looping through each node?

Thanks in advance

Regards.
Posted

1 solution

I don't think this is what you are looking for exactly or the best way to do things, but I thought I'd add it here in case it leads you to a better solution:
C#
private void TestDocument()
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<animal><dog>Doggo</dog><cat>Catsy</cat></animal>");
    AddAttribute(doc.DocumentElement, "xmlns", "");
    StringWriter sw = new StringWriter();
    XmlTextWriter xtw = new XmlTextWriter(sw);
    doc.WriteTo(xtw);
    MessageBox.Show(sw.ToString());
}

private void AddAttribute(XmlNode node, string attribute, string value)
{
    if (node.NodeType == XmlNodeType.Element)
    {
        node.Attributes.Append(node.OwnerDocument.CreateAttribute(attribute));
        node.Attributes[attribute].Value = value;
        foreach (XmlNode subnode in node.ChildNodes)
        {
            AddAttribute(subnode, attribute, value);
        }
    }
}
 
Share this answer
 
Comments
maheshk_blr 4-Feb-11 4:39am    
Thanks, this helped me to resolve my problem, below is the code I have written.

XDocument doc = XDocument.Load(My XML File Path);

XElement rootElement = doc.Element(doc.Root.Name);
XAttribute att = new XAttribute("xmlns", "Namespace name");
XAttribute att1 = new XAttribute("xmlns", "");
rootElement.Add(att);

XElement[] allElements = doc.Elements().ToArray();

foreach (XElement ele in allElements.Nodes()) {
ele.Add(att1);
}

StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
doc.WriteTo(xtw);
MessageBox.Show(sw.ToString());

I would like to remove foreach from the above code segment, yet achieve the same results, is that possible?
AspDotNetDev 5-Feb-11 4:32am    
You can technically do it without a foreach loop, but I don't think this is what you are looking for (I'm not really sure what you're looking for):

new List<xelement>(doc.Descendants()).ForEach((ele) => ele.Add(att));
maheshk_blr 7-Feb-11 5:19am    
Thanks,

Below is my scenario, in which I needed the above. If you can suggest me some better way, please

I am getting an XML file form a source (assume it is a folder), which I need to validate against an XSD file, I am using below code to do that.

List<string> arrerrors = new List<string>();
XDocument doc = XDocument.Load("My XML Path");
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, "My XSD Path");
bool errors = false;

doc.Validate(schemas, (o, e) => {
arrerrors.Add(e.Message);
errors = true;
});

Please note that my XSD has a namespace, which XML does not have and I am trying to manually add it (for that I have asked the above question). If I dont add the namespace to XML, XDocument.Validate() fails to validate the xml. after manually adding the namespace to XML I am getting the xml validated against the XSD.

Since I am very new to XML and XSD, I am not very sure whether my approach is good or not.

Can u please suggest?

Thank you very much for your time and help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900