none
"Attribute is not declared" validation error after XElement to OpenXml conversion RRS feed

  • Question

  • I am converting an XElement to OpenXml using the following code

    internal static OpenXmlElement ToOpenXml(this XElement xel)
    {
        using (var sw = new StreamWriter(new MemoryStream()))
        {
            sw.Write(xel.ToString());
            sw.Flush();
            sw.BaseStream.Seek(0, SeekOrigin.Begin);
    
            var re = OpenXmlReader.Create(sw.BaseStream);
            re.Read();
    
            var oxe = re.LoadCurrentElement();
            re.Close();
            return oxe;
        }
    }

    Prior to conversion, a portion of the Xml looks like this

     <w:p>
          <w:pPr>
            <w:spacing w:before="0" w:after="0" w:line="240" />
            <w:ind w:right="0" w:left="0" w:firstLine="0" />
            <w:jc w:val="left" />
            <w:rPr>
              <w:rFonts w:ascii="Verdana" w:hAnsi="Verdana" w:cs="Verdana" w:eastAsia="Verdana" />
              <w:color w:val="auto" />
              <w:spacing w:val="0" />
              <w:position w:val="0" />
              <w:sz w:val="20" />
              <w:shd w:fill="auto" w:val="clear" />
            </w:rPr>
          </w:pPr>

    After conversion it looks like this

    <w:p>
          <w:pPr>
            <w:spacing w:after="0" w:before="0" w:line="240"/>
            <w:ind w:end="0" w:firstLine="0" w:start="0"/>
            <w:jc w:val="left"/>
            <w:rPr>
              <w:rFonts w:ascii="Verdana" w:cs="Verdana" w:eastAsia="Verdana" w:hAnsi="Verdana"/>
              <w:color w:val="auto"/>
              <w:spacing w:val="0"/>
              <w:position w:val="0"/>
              <w:sz w:val="20"/>
              <w:shd w:fill="auto" w:val="clear"/>
            </w:rPr>
          </w:pPr>


    For some reason the w:ind element has converted from

    <w:ind w:right="0" w:left="0" w:firstLine="0" />

    to

    <w:ind w:end="0" w:firstLine="0" w:start="0"/>

    If i then run the OpenXml through an OpenXmlValidator using 

    var v = new OpenXmlValidator();
    var errs = v.Validate(doc);

    i get numerous validation errors, all of which are

    Description="The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:start' attribute is not declared."

    or

    Description="The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:end' attribute is not declared."

    What am I doing wrong?

    Thursday, October 31, 2019 3:06 PM

All replies