> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/ios/get-started/faq/extract-xml-from-xfa.md).

# How to extract XML from a XFA PDF form?

World's #1 PDF SDK Library for Web, Mobile, Server, Desktop

Suppose the XML data you want to extract is held inside the XFA Array, within the AcroForm dictionary. In order to extract all of the XFA data, you will need to iterate through this Array, and extract all of the content streams.

The following example shows how to extract the XML data at one specific index in the Array:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
// Example code for extracting an xml string from the XFA form,
// and putting it back after an update.

PDFDoc doc = new PDFDoc(filename); 

//get the acroform dictionary
Obj acro_form = doc.GetAcroForm(); 

// This PDF document contains XFA forms... 
Obj obj = acro_form.FindObj("XFA"); 

//We will store the XML string in this byte array
byte[] buff = new byte[4000];
byte byteRawPre, byteDecodePre, byteRawPost, byteDecodePost; 

pdftron.Filters.Filter filter; 
pdftron.Filters.FilterReader fr; 

//The XFA entry in the PDF is an Array, so in this case,
//we want to read the xml string stored at the 5th index of the Array
filter = obj.GetAt(5).GetDecodedStream(); 
fr = new pdftron.Filters.FilterReader(filter); 
fr.Read(buff); 
//at this point, the xml string should be stored inside buff,
//and you can make whatever modifications you want

//Modify XML String HERE

//We create an indirect stream object, which will contain our 
// newly modified XML string
Obj new_xmp_stm = doc.CreateIndirectStream(buff);

//The swap method allows us to switch all indirect references to the old stream,
// to point to our newly created stream.
doc.GetSDFDoc().Swap(
    new_xmp_stm.GetObjNum(), 
    acro_form.Get("XFA").Value().GetAt(5).GetObjNum()
); 

doc.Save(output_filename, SDFDoc.SaveOptions.e_linearized); 
doc.Close();
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/ios/get-started/faq/extract-xml-from-xfa.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
