开发者

How to wrtie a XML License Line(ended with a forward slash '/') in C#?

开发者 https://www.devze.com 2022-12-24 10:43 出处:网络
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\"&

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">
  <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#

<Lice开发者_Python百科nse licenseId="" licensePath="" />

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


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 :(


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


Why don't you just proceed as you started?

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

// Other stuff
textWriter.WriteEndDocument();
textWriter.Close();
0

精彩评论

暂无评论...
验证码 换一张
取 消