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

Free Support Forum - aspose.com

OpenXml MainDocumentPart representation in Aspose Words

Hi,

What would be the Aspose Words code that enumerates StdRun Texts like this using OpenXml API:

// OpenXml API
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

public static IEnumerable<T> Descendants<T>(this MainDocumentPart mainDocumentPart)
    where T : OpenXmlElement
{
    return
        Array.Empty<OpenXmlElement>()
            .Concat(mainDocumentPart.HeaderParts.SelectMany(x => x.Header))
            .Append(mainDocumentPart.Document.Body)
            .Concat(mainDocumentPart.FooterParts.SelectMany(x => x.Footer))
            .SelectMany(x => x.Descendants<T>());
}

using (var document = WordprocessingDocument.Open(templateStream, isEditable: true))
{
    var mainDocumentPart = document.MainDocumentPart;

    mainDocumentPart.Descendants<SdtRun>().SelectMany(x => x.Descendants<Text>()).ForEach(x => x.Text = "");
}

I have tried the one below:

// Aspose API
using Aspose.Words;
using Aspose.Words.Markup;

var document = new Document(templateStream);

document
    .GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>()
    .SelectMany(x => x.GetChildNodes(NodeType.Run, true).Cast<Run>())
    .ForEach(x => x.Text = "");

but it enumerates a little bit different items. Most of them are the same but not all so I need to understand the difference.

@WorkZone

Could you please ZIP and attach your input Word document here for testing? We will investigate the issue and provide you more information on it.

SampleDocument.docx (48.3 KB)

@WorkZone

We have tested the scenario and have not found any issue with Aspose.Words APIs. In your document, all the content controls have Run nodes and there is no Run node with empty text.

Could you please share some detail about the issue that you are facing with Aspose.Words’ APIs?

The problem I have is the difference between the enumerated instances in both versions.
I am migrating OpenXml related code to Aspose.Words and I need to maintain the backward compatibility when processing the documents.

If I add the following lines inside ForEach to the both code samples:

OpenXml:

System.Diagnostics.Trace.WriteLine($"#### OpenXml: {x.Text}");

Aspose.Words

System.Diagnostics.Trace.WriteLine($"#### Aspose: {x.Text}");

and collect the output from both I get:

OpenXml:

#### OpenXml: Title 
#### OpenXml: Created 
#### OpenXml: Sagsbeh. 
#### OpenXml: Name 
#### OpenXml: Overall status date 
#### OpenXml: Approve before sending 
#### OpenXml: Name 
#### OpenXml: Overall status 
#### OpenXml: Workflow instance ID 
#### OpenXml: Name 
#### OpenXml: Case number 
#### OpenXml: Delete original after sending. 
#### OpenXml: Send attachments as separate documents 
#### OpenXml: Summary 
#### OpenXml: Record identity 
#### OpenXml: Summary 
#### OpenXml: Rolle 
#### OpenXml: Summary 
#### OpenXml: External id 
#### OpenXml: Shipment date 
#### OpenXml: Shipment state 
#### OpenXml: Shipment error 
#### OpenXml: Dokumentnr. 
#### OpenXml: Titel 
#### OpenXml: Sagsbeh. 
#### OpenXml: Document number 
#### OpenXml: Title 
#### OpenXml: Case handler 
#### OpenXml: Summary 
#### OpenXml: Summary 
#### OpenXml: Title 
#### OpenXml: Action 
#### OpenXml: Comment 
#### OpenXml: User task opened date 
#### OpenXml: User task closed date 
#### OpenXml: Due date 
#### OpenXml: Page  
#### OpenXml: 1 
#### OpenXml:  of  
#### OpenXml: 1 
#### OpenXml:  
#### OpenXml:  
#### OpenXml:  
#### OpenXml:  

and for Aspose.Words:

#### Aspose:  PAGE  
#### Aspose:  NUMPAGES   
#### Aspose:  
#### Aspose:  
#### Aspose: Title 
#### Aspose: Created 
#### Aspose: Sagsbeh. 
#### Aspose: Name 
#### Aspose: Overall status date 
#### Aspose: Approve before sending 
#### Aspose: Name 
#### Aspose: Overall status 
#### Aspose: Workflow instance ID 
#### Aspose: Name 
#### Aspose: Case number 
#### Aspose: Delete original after sending. 
#### Aspose: Send attachments as separate documents 
#### Aspose: Summary 
#### Aspose: Record identity 
#### Aspose: Summary 
#### Aspose: Rolle 
#### Aspose: Summary 
#### Aspose: External id 
#### Aspose: Shipment date 
#### Aspose: Shipment state 
#### Aspose: Shipment error 
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose: Dokumentnr. 
#### Aspose: Titel 
#### Aspose: Sagsbeh. 
#### Aspose: Document number 
#### Aspose: Title 
#### Aspose: Case handler 
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose: Summary 
#### Aspose: Summary 
#### Aspose: Title 
#### Aspose: :  
#### Aspose: Action 
#### Aspose: Comment 
#### Aspose: User task opened date 
#### Aspose: User task closed date 
#### Aspose: Due date 
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  
#### Aspose:  

These are not the same somehow.

@WorkZone

We have not found the shared issue while using the latest version of Aspose.Words for .NET i.e. 21.6.

In case you are using old version of Aspose.Words for .NET, we suggest you please use the latest version of Aspose.Words for .NET 21.6.

Please also make sure that you are using the same document that you shared in this forum.

You may simply check the Run nodes of document using following simplified code example.

Document document = new Document(MyDir + "SampleDocument.docx");
foreach (Run run in document.GetChildNodes(NodeType.Run, true))
{
        Console.WriteLine($"#### Aspose: {run.Text}");
} 

If you still face problem, please ZIP and attach your working Visual Studio project (source code without compilation errors) here for testing. We will investigate the issue and provide you more information on it.

Does it mean you are getting the same results using Aspose.Words as for OpenXml API?
I am not stating (yet) there is a bug in Aspose.Words but rather I cannot find the correct way to migrate the code from OpenXml.

Anyway, I have attached the sample project that prints out all the matching nodes using OpenXml and Aspose.Words.

AsposeMainDocumentPart.zip (45.7 KB)

@WorkZone

We have logged this problem in our issue tracking system as WORDSNET-22412. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@WorkZone

It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-22412) as ‘Not a Bug’.

Please use the following code example to get the desired output.

static void TestAspose(string fileName)
{
    var document = new Aspose.Words.Document(fileName);

    document
        .GetChildNodes(Aspose.Words.NodeType.Run, true).Cast<Aspose.Words.Run>()
        .Where(x => x.ParentNode.NodeType == NodeType.StructuredDocumentTag)
        .Select(x => x)
        .ForEach(x =>
        {
            Console.Out.WriteLine($"#### Aspose: {x.Text}");
        });
}