We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert InsertCustomXmlPart doesn't work

Hi, I noticed workbook.CustomXmlParts was introduced last year.
I would like to insert a custom XML Parts into Xml.
However, it doesnt seem working.

In Open XML, i can achieve by following code…
public void InsertCustomXmlPart()
{
Uri uriRec = new Uri(string.Format("/customXml/testItem.xml"), UriKind.Relative);
using (SpreadsheetDocument wordprocessingDocument = SpreadsheetDocument.Open(_targetFile.FullName, true))
{
var part = wordprocessingDocument.Package.CreatePart(uriRec, “application/vnd.openxmlformats-officedocument.spreadsheetml”);

            using (Stream stream = part.GetStream())
            {
                byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
                stream.Write(buf, 0, buf.Length);
            }

            wordprocessingDocument.Save();
        }
    }

However, i don’t think i can achieve something similar with Aspose. Can you please let me know how I can achieve with Aspose?

@catalparue26,

Thanks for your query.

Please see the following sample code for your reference:
e.g
Sample code:

Workbook workbook = new Workbook("f:\\files\\odata_query.xlsx");
            byte[] data = workbook.CustomXmlParts[0].Data;
            FileStream fstream = new System.IO.FileStream("f:\\files\\item1.xml", FileMode.OpenOrCreate);
            fstream.Write(data, 0, data.Length);

Hope, this helps a bit.

do you have something like workbook.CustomXmlParts.Add(…) to add the new custom xml rather than overwrite existing xml?

@catalparue26,
Aspose.Cells provides features to add custom XML parts. Please visit the following link to get examples for adding custom XML parts and share the feedback.

Add Custom XML Parts and Select them by ID

thanks. Can you please provide me an example? what is schema data? can i leave it null?

@catalparue26,
We are working on your requirement and will share our feedback soon. Your requirement is logged as follows:

CELLSNET-47220 - Valid schema example required for adding custom XML parts

Thanks. the following is my test code… I do not think CustomXmlParts.Add() is working…
The following code do not insert anything into \customXml folder.

  const string docXml =
        @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> 
                            <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
                                <w:body>
                                    <w:p>
                                        <w:r>
                                            <w:t>Hello world!</w:t>
                                        </w:r>
                                    </w:p>
                                </w:body>
                            </w:document>";
    public void InsertCustomXmlPart()
    {
        var buf = (new UTF8Encoding()).GetBytes(docXml);
        _workbook.CustomXmlParts.Add(buf, buf);
    }

@catalparue26,
Thank you for the feedback. We have recorded this information with the logged ticket for our reference. We will write back here as soon as some feedback is ready to share.

thanks! Hope we can have the feedback soon!

@catalparue26,
1: Please check the attached files.
btsDatais itemProps2.xml.
btsSchema is item2.xml.

2: Id should be same as guid:
CustomXmlPart part = workbook.CustomXmlParts.SelectByID(“2F087CB2-7CA8-43DA-B048-2E2F61F4936F”);
Assert.AreEqual(“2F087CB2-7CA8-43DA-B048-2E2F61F4936F”,part.ID);
string x = “2F087CB2-7CA8-43DA-B048-2E2F61F0000F”;
part.ID = x;

customxmlpart.zip (1.5 KB)

Hi, I am confused by your answwer. I question is how to add an CustomXML. not how to select.
My question is that the following method does not work. your code example show me how to select customXML and compare which is not my question.

const string docXml =
@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body>
<w:p>
<w:r>
<w:t>Hello world!</w:t>
</w:r>
</w:p>
</w:body>
</w:document>";
public void InsertCustomXmlPart()
{
var buf = (new UTF8Encoding()).GetBytes(docXml);
_workbook.CustomXmlParts.Add(buf, buf);
}

My question is very simple, can you please provide me an Sample Code of using
_workbook.CustomXmlParts.Add(xxx, xx); The following link do not have valid Sample Code. I tested by above code and it does not work.
Add Custom XML Parts and Select them by ID

@catalparue26,
We are working on your requirement and will share our feedback soon.

@catalparue26,
Please check the doc : https://docs.microsoft.com/en-us/visualstudio/vsto/how-to-add-custom-xml-parts-to-document-level-customizations?view=vs-2019

With the following codes:

const string docXml = @"<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""><w:body ><w:p><w:r><w:t> Hello world!</w:t></w:r ></w:p></w:body></w:document>";
Workbook workbook = new Workbook();
var buf = (new UTF8Encoding()).GetBytes(docXml);
workbook.CustomXmlParts.Add(buf, null);
workbook.Save(dir + "dest.xlsx");

Let us know your feedback.

hi, i know i can use other framework like OpemXML or vsto for this. I tested other framework and it looks fine.
The reason i ask you is i want to know if ASPOSE is able to deal with it. We do not want to import lots of third party library in one project. We want ASPOSE to do everything.
ASPOSE already have functionality for _workbook.CustomXmlParts.Add(xxx, xx). I would expect you are able to give me the code example for this.
Again, Can you please give me ASPOSE example?

Hi, Just simply give me an Aspose Example : _workbook.CustomXmlParts.Add(xxx, xx).
If it is not completed, just let us know when it is the time you can complete this functionality.

@catalparue26,

I have tried the following sample code to add the custom XML part to a workbook.

    string xmlString =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<employees>" +
    "<employee>" +
        "<name>Karina Leal</name>" +
        "<hireDate>1999-04-01</hireDate>" +
        "<title>Manager</title>" +
    "</employee>" +
"</employees>";

    Workbook workbook = new Workbook();
    var buf = (new UTF8Encoding()).GetBytes(xmlString);
    workbook.CustomXmlParts.Add(buf, null);
    workbook.Save("xmlString.xlsx");

When this XLSX file is analyzed, it contains item1.xml in customXML folder as follows:
image.png (41.4 KB)

Could you please try this example and if it does not fulfil your requirement, share your expected output file created by some third party tool for our reference. We will analyze it and try to provide you assistance using Aspose.Cells.

We are sorry for any inconvenience caused to you in this regard.

hi, thanks! it works great! :slight_smile:

@catalparue26,
Good to know that your issue is sorted out by the suggested sample code. Feel free to contact us at any time if you need further help or have some other issue or queries, we will be happy to assist you soon.