views:

118

answers:

3

I want to write a XML file as below:

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  <License licenseId="" licensePath="" />

Some piece of my code attached here

    // Create a new file in D:\\ and set the encoding to UTF-8
    XmlTextWriter textWriter = new XmlTextWriter("D:\\books.xml", System.Text.Encoding.UTF8);

    // Format automatically
    textWriter.Formatting = Formatting.Indented;

    // Opens the document
    textWriter.WriteStartDocument();

    // Write the namespace declaration.
    textWriter.WriteStartElement("books", null);
    // Write the genre attribute.
    textWriter.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
    textWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");

And now I need to write the License Line below in C#

<License licenseId="" licensePath="" />

But I don't know how to move on for I found the Line ended with the forward slash / .Thank you.

+1  A: 

Calling the WriteEndElement method will automatically take care of adding the forwards slash.

logicnp
textWriter.WriteEndElement(); // Works well.
Nano HE
+2  A: 

I have 2 questions about the way you're doing this:

1) Do you have to use a text writer? If you have access to c# 3.0 then you can use the following:

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    new XElement("Equipment",
        new XElement("License", 
            new XAttribute("licenseId", ""), 
            new XAttribute("licensePath", "")
        )
    )
);

2) Do you have to declare the two namespaces? It seems to me like you won't use them:

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement("Equipment",
        new XElement("License", 
            new XAttribute("licenseId", ""), 
            new XAttribute("licensePath", "")
        )
    )
);

If you're intending to write multiple License elements to the document, and you have them in an Array, List or some other IEnumerable, you can use something similar to the code below to spit them all out:

IEnumerable<LicenceObjects> licenses = //some code to make them;

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement("Equipment",
        licenses.Select(l => 
            new XElement("License", 
                new XAttribute("licenseId", l.licenseId), 
                new XAttribute("licensePath", l.licensePath)
            )
        )
    )
);

string xmlDocumentString = doc.ToString();

Of course, if you don't have .NET 3.0, then this is useless to you :(

Codesleuth
Hi Codesleuth. (+1) I must use .net 2.0 for my project currently. But your solution is very usefully for my coming study. Thank you.
Nano HE
+1  A: 

Why don't you just proceed as you started?

textWriter.WriteStartElement("Licence");
textWriter.WriteAttributeString("LicenseId", "");
textWriter.WriteAttributeString("LicensePath", "");

// Other stuff
textWriter.WriteEndDocument();
textWriter.Close();
Eric Eijkelenboom
Hi Eric, I must end the `license` line with the forward slash **/**. thank you.
Nano HE
This automatically taken care of by the code above.
Eric Eijkelenboom