6

I want to create a xml document and root element like this:

<rdf:RDF xmlns:cim="http://iec.ch/TC57/2009/CIM-schema-cim14#"

xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

I try create this like that:

 XmlDocument doc = new XmlDocument();
        XmlNode rootNode = doc.CreateElement("rdf:RDF xmlns:cim="http://iec.ch/TC57/2009/CIM-schema-cim14#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">");
        doc.AppendChild(rootNode);

        XmlNode userNode = doc.CreateElement("user");
        XmlAttribute attribute = doc.CreateAttribute("age");
        attribute.Value = "42";
        userNode.Attributes.Append(attribute);
        userNode.InnerText = "John Doe";
        rootNode.AppendChild(userNode);

        userNode = doc.CreateElement("user");
        attribute = doc.CreateAttribute("age");
        attribute.Value = "39";
        userNode.Attributes.Append(attribute);
        userNode.InnerText = "Jane Doe";
        rootNode.AppendChild(userNode);

        doc.Save("C:/xml-test.xml");

But i have exeption :The ' ' character, hexadecimal value 0x20, cannot be included in a name. Or so on.

How to make this element? Thanks.

6
  • 2
    I'm going to cry soon... This is NOT how you create XML. Commented Aug 9, 2013 at 7:29
  • 4
    Crying is not going to help. Commented Aug 9, 2013 at 7:33
  • Do you specifically want to build the XmlDocument element-by-element like that, or could you just use XmlDocument.LoadXml()? Commented Aug 9, 2013 at 7:37
  • I want to create XML document element by element from my List<MyObject> and from it properties; Commented Aug 9, 2013 at 7:39
  • 1
    You might also want to look into XmlSerialization which can take a list of objects and generate XML for them - also worth looking at xsd.exe that can take the appropriate XSD file, and generate C# (or VB.Net) classes for you that will serialise/deserialise via XmlSerialisation. Commented Aug 9, 2013 at 7:46

2 Answers 2

3

The method you're using for building XML is actually building a tree of objects (rather than as the textual representation of them), for for Schemas, you have to tell the document about them:

XmlDocument doc = new XmlDocument();
XmlSchemaSet xss = new XmlSchemaSet();
xss.Add("cim", "http://iec.ch/TC57/2009/CIM-schema-cim14#");
xss.Add("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
doc.Schemas = xss;
XmlNode rootNode = doc.CreateElement("rdf:RDF"); // This overload assumes the document already knows about the rdf schema as it is in the Schemas set
doc.AppendChild(rootNode);
Sign up to request clarification or add additional context in comments.

Comments

1

If you can consider using Linq to XML, here's an alternative.

// Your data
var users = new List<User> {
    new User { Name = "John", Age = 42 },
    new User { Name = "Jane", Age = 39 }
};

// Project the data into XElements
var userElements = 
    from u in users     
    select 
        new XElement("user", u.Name, 
            new XAttribute("age", u.Age));

// Build the XML document, add namespaces and add the projected elements
var doc = new XDocument(
    new XElement("RDF",
        new XAttribute(XNamespace.Xmlns + "cim", 
            XNamespace.Get("http://iec.ch/TC57/2009/CIM-schema-cim14#")),
        new XAttribute(XNamespace.Xmlns + "rdf", 
            XNamespace.Get("http://www.w3.org/1999/02/22-rdf-syntax-ns#")),
        userElements
    )
);      

doc.Save(@"c:\xml-test.xml");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.