Click here to Skip to main content
15,883,905 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
dear friends

i want to add the validation info in my xml file like this

XML
<?xml version="1.0" encoding="utf-8"?>
<ROOTElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="yourschema.xsd">


how can i do this in my c# code
Posted

1 solution

This shoud work

C#
using System.Xml;

namespace Dummy
{
    public class Sample
    {
        public void GenerateXmlDocument()
        {
            XmlDocument xdoc = new XmlDocument();

            XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);

            xdoc.AppendChild(dec);

            XmlNode rootNode = xdoc.CreateNode(XmlNodeType.Element, "ROOTElement", "");

            XmlAttribute att1 = xdoc.CreateAttribute("xmlns:xsi");
            att1.Value = "http://www.w3.org/2001/XMLSchema-instance";
            rootNode.Attributes.Append(att1);

            XmlAttribute att2 = xdoc.CreateAttribute("xsi:noNamespaceSchemaLocation");
            att2.Value = "yourschema.xsd";
            rootNode.Attributes.Append(att2);

            xdoc.AppendChild(rootNode);

            xdoc.Save(@"c:\sample.xml");
        }
    }
}


- Modded code to show adding a child element

C#
using System.Xml;

namespace Dummy
{
    public class Sample
    {
        public void GenerateXmlDocument()
        {
            XmlDocument xdoc = new XmlDocument();

            XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);

            xdoc.AppendChild(dec);

            XmlNode rootNode = xdoc.CreateNode(XmlNodeType.Element, "ROOTElement", "");

            XmlAttribute att1 = xdoc.CreateAttribute("xmlns:xsi");
            att1.Value = "http://www.w3.org/2001/XMLSchema-instance";
            rootNode.Attributes.Append(att1);

            XmlAttribute att2 = xdoc.CreateAttribute("xsi:noNamespaceSchemaLocation");
            att2.Value = "yourschema.xsd";
            rootNode.Attributes.Append(att2);

            xdoc.AppendChild(rootNode);

            XmlElement XElemRoot = xdoc.CreateElement("ACES");
            rootNode.AppendChild(XElemRoot);
            XmlElement Xsource = xdoc.CreateElement("RETURN");
            Xsource.SetAttribute("ReturnType", "DLR");
            Xsource.SetAttribute("ToolVer", "1.0");
            XElemRoot.AppendChild(Xsource);
            xdoc.Save(@"c:\sample.xml");
        }
    }
}


This gives

XML
<?xml version="1.0" encoding="utf-8"?>
<ROOTElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="yourschema.xsd">
  <ACES>
    <RETURN ReturnType="DLR" ToolVer="1.0" />
  </ACES>
</ROOTElement>
 
Share this answer
 
v2
Comments
srilekhamenon 17-Jan-12 6:05am    
Thanks but it is working fine but now i want to add more node
XmlElement XElemRoot = XDoc.CreateElement("ACES");
XDoc.AppendChild(XElemRoot);
XmlElement Xsource = XDoc.CreateElement("RETURN");
Xsource.SetAttribute("ReturnType", "DLR");
Xsource.SetAttribute("ToolVer", "1.0");
XElemRoot.AppendChild(Xsource);
but iam geting error in line 2 This document already has a 'DocumentElement' node. how to solve this error
Reiss 17-Jan-12 6:36am    
You can't add a second element directly to an xml docuemnt, it would need to be nested inside a single root element - I have modified my example to show you how this can be done
srilekhamenon 17-Jan-12 7:10am    
Thankx Reiss, You have saved a day of mine, ThankU very much

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