简体   繁体   中英

how to add namespace using XmlWriter in xml document

Hi I have to save this file in xfdf file in C# using XmlWriter Class;

using (var fs = File.Open("D://abc.xfdf", FileMode.Create))
{
    try
    {
        var doc = XmlWriter.Create(fs);
        doc.WriteStartElement("Highlights");
        foreach (var h in Highlights)
        {
            doc.WriteStartElement("Highlight");
            doc.WriteElementString("Id", h.Id);
            doc.WriteEndElement();
        }
        doc.WriteEndElement();
        doc.Flush();
    }
}

But I am not able save in xfdf file. getting problem to adding

<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">

Your link doesn't really explain what your problem is, but I'll take a sample bit of XML from there and walk you through creating it. The principles can be applied to whichever elements you're actually trying to create.

<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
    <f href="Document.pdf"/>
    <fields>
        <field name="Street">
            <value>345 Park Ave.</value>
        </field>        
    </fields>
</xfdf> 

So, while you can do this with XmlWriter directly, this is generally not a good idea - it's very low level and as a result not very nice to read or write. For example, this is the code it would take to just create the outer element and the first child. Notice how you have to be very careful to match writing the start and end of elements:

writer.WriteStartElement("xfdf", "http://ns.adobe.com/xfdf/");
writer.WriteAttributeString("space", "http://www.w3.org/XML/1998/namespace", "preserve");
writer.WriteStartElement("f", "http://ns.adobe.com/xfdf/");
writer.WriteAttributeString("href", "Document.pdf");
writer.WriteEndElement();
writer.WriteEndElement();

Alternatively, you can use the much higher level, cleaner LINQ to XML API to declaratively create your XML:

XNamespace ns = "http://ns.adobe.com/xfdf/";

var doc = new XDocument(
    new XElement(ns + "xfdf",
        new XAttribute(XNamespace.Xml + "space", "preserve"),
        new XElement(ns + "f",
            new XAttribute("href", "Document.pdf")
            ),
        new XElement(ns + "fields",
            new XElement(ns + "field",
                new XAttribute("name", "Street"),
                new XElement(ns + "value",
                    "345 Park Ave."
                    )
                )
            )
        )
    );

doc.Save(@"D:\abc.xdfd");

You can use the API to add elements from a sequence in a variety of different ways, such as:

var element = new XElement(ns + "highlights");

foreach (var h in highlights)
{
    element.Add(new XElement(ns + "highlight", h.Id));
}

Or:

var element = new XElement(ns + "highlights",
    highlights.Select(h => new XElement(ns + "highlight", h.Id))
    );

As ever, Google is your friend. There are lots of examples on how to use LINQ to XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM