3

I am creating a XML document in c#.

Its all coming together now but for some reason i keep getting tis error.

Here is my code:

  FileStream Slide2XmlStream = new FileStream(slide2Xml, FileMode.Create, FileAccess.Write);
        XmlWriter slide2XmlWriter = null;

        try
        {
            XmlWriterSettings contentTypesSettings = new XmlWriterSettings();
            contentTypesSettings.Indent = true;
            slide2XmlWriter = XmlWriter.Create(Slide2XmlStream, contentTypesSettings);

            slide2XmlWriter.WriteStartDocument(true);
            slide2XmlWriter.WriteStartElement( "p", "sld","http://schemas.openxmlformats.org/presentationml/2006/main" );
            slide2XmlWriter.WriteAttributeString("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            slide2XmlWriter.WriteAttributeString( "a","http://schemas.openxmlformats.org/drawingml/2006/main");

            slide2XmlWriter.WriteStartElement( "p", "cSld", null);

            slide2XmlWriter.WriteStartElement( "p", "spTree", null );
            slide2XmlWriter.WriteStartElement( "p", "nvGrpSpPr", null );
            slide2XmlWriter.WriteStartElement( "p", "cNvpr", null );
            slide2XmlWriter.WriteAttributeString("name", "");
            slide2XmlWriter.WriteAttributeString("id", "1");
            slide2XmlWriter.WriteEndElement();

            slide2XmlWriter.WriteStartElement( "p", "cNvGrpSpPr", null );
            slide2XmlWriter.WriteEndElement();

            slide2XmlWriter.WriteStartElement( "p", "nvPr", null );
            slide2XmlWriter.WriteEndElement();
            slide2XmlWriter.WriteEndElement();

            slide2XmlWriter.WriteStartElement( "p", "grpSpPr", null );
            slide2XmlWriter.WriteStartElement( "a", "xfrm", null );
            slide2XmlWriter.WriteStartElement( "a", "off", null );
            slide2XmlWriter.WriteAttributeString("y", "0");
            slide2XmlWriter.WriteAttributeString("x", "0");
            slide2XmlWriter.WriteEndElement();

The error keeps appearing on the "a" prefix on the start of the method. Any help will really be appreaciated.

2
  • 1
    What do you mean "on the start of the method"? It would really help if you could write a short but complete program which demonstrate the problem - just with the call that fails, if possible. (We don't know which of your many method calls is failing.) Commented May 29, 2012 at 9:45
  • 1
    My guess translation of wuestion would be: this is all my code from method, I'am getting error Cannot use a prefix with an empty namespace on the beginning of method, on line slide2XmlWriter.WriteAttributeString( "a","http://schemas.openxmlformats.org/drawingml/2006/main"); Commented May 29, 2012 at 9:49

3 Answers 3

1

In XML, an element may either be in a namespace, or not (which is considered a backward considered mode, sometimes also called "null namespace"). If you want it in a null namespace, which is what you seem to be requesting near the end of the method, then you cannot request a namespace prefix. There is no way to bind a namespace prefix to a "null namespace".

The converse is possible. There may be no prefix and the element may either be in a namespace (default namespace), or not in a namespace (null namespace). This depends on the presence of the xmlns attribute in the serialized XML document.

Sign up to request clarification or add additional context in comments.

Comments

1

I have fixed it. I needed to allow the xmlns prefix and allow the string value to be null:

slide2XmlWriter.WriteAttributeString("xmlns","a",null,"http://schemas.openxmlformats.org/drawingml/2006/main");

Comments

0

In case you are using XmlSerializer and you are getting this exception make sure you do not have any ShouldSerializeXXX methods in your classes for xml attributes.

For instance, do not do this:

[XmlRoot(ElementName = "Foo")]
public class Foo
{
    [XmlAttribute(AttributeName = "Bar")]
    public string Bar{ get; set; }
    
    public bool ShouldSerializeBar(){ return true; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.