none
Validating a WordprocessingDocument RRS feed

  • Question

  • I'm building a document using the OpenXmlSDK 2.0. The document passes the validation in:

     

    OpenXmlPackageValidationSettings validate = new OpenXmlPackageValidationSettings();
    validate.EventHandler +=
    new EventHandler<OpenXmlPackageValidationEventArgs>(validate_EventHandler);
    Document.Validate(validate);

    When I try to open the document after it has been saved, Word tells me "The file ... cannot be opened because there are problems with the content." The detail gives me "Unspecified error Location Part: /word/document2.xml, Line:1, Column: 1715"

    If I tell Word to recover the document it opens.

    This only happens when I have images in my document. I'm using the following code to add the image to the document:

    The MainDocument part contains the Xml for an image that includes Blip.
    string id = string.Format("rId{0}", Document.MainDocumentPart.Parts.Count() + 1);
    blip.Attribute(ns_r +
    "id").Value = id;
    //add the image part to the target document
    ImagePart imagePart = Document.MainDocumentPart.AddImagePart(imagePartType, id);
    MemoryStream imageStream = new MemoryStream(imageBytes);
    imagePart.FeedData(imageStream);


    How can I find the problem in the XML?


    Ockert
    Tuesday, November 24, 2009 10:32 PM

All replies

  • To insert an image, itis not enough to just insert an image part. You would also need to add content in the MainDocument part (or some other part) to reference this image pat. 
    You might try to use the tools and compare 2 documents that one has image added by Office app and the other one has image added by your code.
    Wednesday, November 25, 2009 10:51 AM
  • I've successfully added images to documents using the OpenXmlSDK. The problem was that there was an OLEObject object type included in documents with VML based images which I was not aware off.

    Using the ValidationErrorInfo to validate and trap any errors is useful. The problem is that I’m now getting the following error which seems to be wrong. The “tr” element may be a child element of the “tbl” element.

    Description “The element has unexpected child element 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:tr'. List of possible elements expected: <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlInsRangeEnd>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:bookmarkStart>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:bookmarkEnd>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:commentRangeStart>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:commentRangeEnd>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:moveFromRangeStart>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:moveFromRangeEnd>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:moveToRangeStart>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:moveToRangeEnd>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlInsRangeStart>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlDelRangeStart>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlDelRangeEnd>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlMoveFromRangeStart>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlMoveFromRangeEnd>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlMoveToRangeStart>,
    <http://schemas.openxmlformats.org/wordprocessingml/2006/main:customXmlMoveToRangeEnd>.”

    Path: “/w:document[1]/w:body[1]/w:tbl[1]”


    Ockert
    Wednesday, November 25, 2009 1:35 PM
  • Hi Ockert, "tr" could be a child elemen of "tbl". However, the child elements of the "tbl" needs to be in the right sequence and "tr" might come too early as "unexpected".

    Friday, November 27, 2009 6:26 AM
  • Is this table generated by code?
    There must be one <w:tblPr> and one <w:tblGrid> element before the <w:tr> element under <w:tbl>.

    <w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:tblPr>
        <w:tblStyle w:val="TableGrid" />
        <w:tblW w:w="0" w:type="auto" />
        <w:tblLook w:val="04A0" />
      </w:tblPr>
      <w:tblGrid>
        <w:gridCol w:w="4261" />
        <w:gridCol w:w="4261" />
      </w:tblGrid>
      <w:tr w:rsidR="006D071D" w:rsidTr="006D071D">
        <w:tc>
        ......

    Just create one simple document with a table in Word. And you will see these 2 elements in xml.
    Monday, November 30, 2009 10:24 AM