Answers for "how to create xml file"

1

xml.etree create xml file

#code :
import xml.etree.cElementTree as ET

root = ET.Element("root")
doc = ET.SubElement(root, "doc")

ET.SubElement(doc, "field1", name="blah").text = "some value1"
ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2"

tree = ET.ElementTree(root)
tree.write("filename.xml")

#output : filename.xml -> 
"""
<root>
    <doc>
         <field1 name="blah">some value1</field1>
         <field2 name="asdfasd">some vlaue2</field2>
    </doc>
</root>
""""
Posted by: Guest on November-25-2020
1

how to create xml file in c#

private XmlElement CreateLicenseKeyValuePairNodes(XmlDocument document)
        {

            XmlElement keyValueElement = document.CreateElement("KeyValuePair");
            XmlElement keyElement = document.CreateElement("Key");
            XmlElement valueElement = document.CreateElement("Value");

            keyValueElement.AppendChild(keyElement);
            keyValueElement.AppendChild(valueElement);

            return keyValueElement;
        }


        protected virtual void CreateXmlDoc(int count)
        {
            XmlDocument outputDocument = new XmlDocument();
            XmlElement rootElement = outputDocument.CreateElement("Base");
            XmlElement customerElement = outputDocument.CreateElement("Customer");
            XmlElement projectElement = outputDocument.CreateElement("Project");
            XmlElement programIdElement = outputDocument.CreateElement("ProgramID");
            XmlElement activationIdElement = outputDocument.CreateElement("ActivationID");
            XmlElement featureElement = outputDocument.CreateElement("Features");


            XmlElement releaseElement = outputDocument.CreateElement("ReleaseDate");
            XmlElement validationElement = outputDocument.CreateElement("ValidUntil");
            rootElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            rootElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");

            rootElement.AppendChild(customerElement);
            rootElement.AppendChild(projectElement);
            rootElement.AppendChild(programIdElement);
            rootElement.AppendChild(activationIdElement);
            rootElement.AppendChild(featureElement);

            for (int i = 0; i < count; i++)
            {
                featureElement.AppendChild(CreateLicenseKeyValuePairNodes(outputDocument));
            }

            rootElement.AppendChild(releaseElement);
            rootElement.AppendChild(validationElement);
            outputDocument.AppendChild(rootElement);

            outputDocument.Save(@".Template.xml");
        }
Posted by: Guest on October-22-2020

Python Answers by Framework

Browse Popular Code Answers by Language