G
Guest
I am trying to insert the following xml fragment into a WordML document programmatically:
<ns0:catalog>
....
<ns0:item>
<ns0:title wlaceholder="Enter item title.">
<w>
<wPr>
<wStyle w:val="Heading2" />
</wPr>
</w>
</ns0:title>
<ns0:description wlaceholder="Enter item description.">
<w>
<wPr>
<wStyle w:val="Normal" />
</wPr>
</w>
</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
<ns0:catalog>
....
<ns0:item>
<ns0:title wlaceholder="Enter item title.">
<w>
<wPr>
<wStyle w:val="Heading2" />
</wPr>
</w>
</ns0:title>
<ns0:description wlaceholder="Enter item description.">
<w>
<wPr>
<wStyle w:val="Normal" />
</wPr>
</w>
</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