Inserting block level xml fragments.

G

Guest

I am trying to insert the following xml fragment into a WordML document programmatically:

<ns0:catalog>
....
<ns0:item>
<ns0:title w:placeholder="Enter item title.">
<w:p>
<w:pPr>
<w:pStyle w:val="Heading2" />
</w:pPr>
</w:p>
</ns0:title>
<ns0:description w:placeholder="Enter item description.">
<w:p>
<w:pPr>
<w:pStyle w:val="Normal" />
</w:pPr>
</w:p>
</ns0:description>
<ns0:item>
....

</ns0:catalog>


But, i cannot achieve the result no matter what I try.
I am getting errors like Xml node cannot be inserted at current selection, null object reference exceptions, etc.
Title item gets created and than it appears deleted when InsertParagraphAfter() is called(can see it when the revision tracking is on, plus XmlBeforeDelete event fires), which violates the document schema.
Plus, another paragraph gets created between the item and the title nodes.
I am sick and tired of fighting with the paragraphs already.
Did anyone else run into this kind of issues?
Does anyone have a solution or knows how to overcome this?

here is the code that I use:

/// <summary>

/// Adds new item xml node

/// </summary>

internal Word.XMLNode AddItem(Word.XMLNode parentNode, string titlePlaceholderText, string descriptionPlaceholderText)

{

Word.XMLNode itemNode = null;


object start = parentNode.Range.End - 1;

object end = parentNode.Range.End - 1;

object rng = this.ThisDocument.Range(ref start, ref end);


itemNode = parentNode.XMLNodes.Add("title", this.XmlNamespace, ref rng);

if (itemNode==null)

{

throw new ExecutionEngineException("Unable to create item node");

}


itemNode.Range.InsertParagraph();


//TODO: Set paragraph style


rng = itemNode.Range;


Word.XMLNode titleNode = itemNode.XMLNodes.Add("title", this.XmlNamespace, ref rng);

if (titleNode==null)

{

throw new ExecutionEngineException("Unable to create title node");

}


titleNode.PlaceholderText = "Enter title here";


titleNode.Range.InsertParagraphAfter();


//TODO: Set paragraph style

start = titleNode.Range.End;

end = itemNode.Range.End;


rng = this.ThisDocument.Range(ref start, ref end);


Word.XMLNode descriptionNode = itemNode.XMLNodes.Add("description", this.XmlNamespace, ref rng);

if (descriptionNode==null)

{

throw new ExecutionEngineException("Unable to create description node");

}


descriptionNode.PlaceholderText = "Enter title here";

titleNode.Range.Select();

return itemNode;

}





Sincerely,
Ruslan
(nospam)newsgroups(nospam)(at)(nospam)ruslan(nospam)(dot)ca
 
B

Beth Melton

If you don't get the answer you are looking for here you might try one of the developer newsgroups since that's where the VBA and XML gurus tend to hang out. :)

--
Please post all follow-up questions to the newsgroup. Requests for assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/


I am trying to insert the following xml fragment into a WordML document programmatically:

<ns0:catalog>
...
<ns0:item>
<ns0:title w:placeholder="Enter item title.">
<w:p>
<w:pPr>
<w:pStyle w:val="Heading2" />
</w:pPr>
</w:p>
</ns0:title>
<ns0:description w:placeholder="Enter item description.">
<w:p>
<w:pPr>
<w:pStyle w:val="Normal" />
</w:pPr>
</w:p>
</ns0:description>
<ns0:item>
...

</ns0:catalog>


But, i cannot achieve the result no matter what I try.
I am getting errors like Xml node cannot be inserted at current selection, null object reference exceptions, etc.
Title item gets created and than it appears deleted when InsertParagraphAfter() is called(can see it when the revision tracking is on, plus XmlBeforeDelete event fires), which violates the document schema.
Plus, another paragraph gets created between the item and the title nodes.
I am sick and tired of fighting with the paragraphs already.
Did anyone else run into this kind of issues?
Does anyone have a solution or knows how to overcome this?

here is the code that I use:

/// <summary>

/// Adds new item xml node

/// </summary>

internal Word.XMLNode AddItem(Word.XMLNode parentNode, string titlePlaceholderText, string descriptionPlaceholderText)

{

Word.XMLNode itemNode = null;


object start = parentNode.Range.End - 1;

object end = parentNode.Range.End - 1;

object rng = this.ThisDocument.Range(ref start, ref end);


itemNode = parentNode.XMLNodes.Add("title", this.XmlNamespace, ref rng);

if (itemNode==null)

{

throw new ExecutionEngineException("Unable to create item node");

}


itemNode.Range.InsertParagraph();


//TODO: Set paragraph style


rng = itemNode.Range;


Word.XMLNode titleNode = itemNode.XMLNodes.Add("title", this.XmlNamespace, ref rng);

if (titleNode==null)

{

throw new ExecutionEngineException("Unable to create title node");

}


titleNode.PlaceholderText = "Enter title here";


titleNode.Range.InsertParagraphAfter();


//TODO: Set paragraph style

start = titleNode.Range.End;

end = itemNode.Range.End;


rng = this.ThisDocument.Range(ref start, ref end);


Word.XMLNode descriptionNode = itemNode.XMLNodes.Add("description", this.XmlNamespace, ref rng);

if (descriptionNode==null)

{

throw new ExecutionEngineException("Unable to create description node");

}


descriptionNode.PlaceholderText = "Enter title here";

titleNode.Range.Select();

return itemNode;

}





Sincerely,
Ruslan
(nospam)newsgroups(nospam)(at)(nospam)ruslan(nospam)(dot)ca
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top