1

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">
3
  • 1
    Hi, just look up my post Commented Jun 19, 2015 at 11:09
  • What does xfdf have to do with Highlights and Id? You need to explain how your code relates to your XML and what the problem is. As an aside, I wouldn't write XML like this unless you have a very good reason (like this XML is huge). Look at LINQ to XML. Commented Jun 19, 2015 at 11:12
  • I have to create the xfdf file Like please chek this link:-partners.adobe.com/public/developer/en/xml/xfdf_2.0.pdf: Page No-8 Commented Jun 19, 2015 at 11:19

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks Charles, I am not able to add this attribute in xfdf element (xml:space="preserve"). getting error like this:-Invalid name character in 'xml:space'. The ':' character, hexadecimal value 0x3A, cannot be included in a name.
Sorry, missed that - added now.

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.