XmlWriter with ConformanceLevel.Fragment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shimon Sim

    XmlWriter with ConformanceLevel.Fragment

    Hi
    I keep getting error :

    WriteStartDocum ent cannot be called on writers created with
    ConformanceLeve l.Fragment.

    The code that I have is very simple.

    SimpleClass c = new SimpleClass();

    c.Name = "TestName";

    XmlSerializer serializer = new XmlSerializer(t ypeof(SimpleCla ss));


    MemoryStream stream =new MemoryStream();

    XmlWriterSettin gs settings = new XmlWriterSettin gs();

    settings.OmitXm lDeclaration = true;

    settings.Confor manceLevel = ConformanceLeve l.Fragment;


    settings.Encodi ng = Encoding.ASCII;

    XmlWriter writer = XmlWriter.Creat e(stream,settin gs);

    serializer.Seri alize(writer,c) ;

    stream.Position = 0;

    byte[] data=new byte[stream.Length];

    stream.Read(dat a, 0, (int)stream.Len gth);


    result = new string(Encoding .Default.GetCha rs(data));

    I found in documentation that if there is DTD information that
    WriteStartDocum ent will be called and with ConformanceLeve l.Fragment writer
    will throw an exception.

    So where is DTD information? Can I remove it to make this work?

    Thanks.

    Shimon


  • Martin Honnen

    #2
    Re: XmlWriter with ConformanceLeve l.Fragment

    Shimon Sim wrote:
    I keep getting error :
    >
    WriteStartDocum ent cannot be called on writers created with
    ConformanceLeve l.Fragment.
    >
    The code that I have is very simple.
    >
    SimpleClass c = new SimpleClass();
    >
    c.Name = "TestName";
    >
    XmlSerializer serializer = new XmlSerializer(t ypeof(SimpleCla ss));
    >
    >
    MemoryStream stream =new MemoryStream();
    >
    XmlWriterSettin gs settings = new XmlWriterSettin gs();
    >
    settings.OmitXm lDeclaration = true;
    >
    settings.Confor manceLevel = ConformanceLeve l.Fragment;
    >
    >
    settings.Encodi ng = Encoding.ASCII;
    >
    XmlWriter writer = XmlWriter.Creat e(stream,settin gs);
    >
    serializer.Seri alize(writer,c) ;
    I guess the Serialize method of XmlSerializer calls the
    WriteStartDocum ent method of XmlWriter. Why exactly do you set
    ConformanceLeve l.Fragment, what do you want to achieve with that setting?


    --

    Martin Honnen --- MVP XML

    Comment

    • Shimon Sim

      #3
      Re: XmlWriter with ConformanceLeve l.Fragment

      I want to serialize class without header and without any default namespaces.
      Header could be easily removed as you showed me earlier but to remove
      namespaces that doesn't help. I did find one solution to go through
      XmlDocument and extract a node from it.
      But I wanted to do it directly using XmlWriter. I looked into custom
      XmlWriters but didn't see any fast solution. So I thought that if I will use
      Fragment it will not add any namespaces.
      Thanks,
      Shimon.
      "Martin Honnen" <mahotrash@yaho o.dewrote in message
      news:u$C71ijkHH A.4008@TK2MSFTN GP03.phx.gbl...
      Shimon Sim wrote:
      >
      >I keep getting error :
      >>
      >WriteStartDocu ment cannot be called on writers created with
      >ConformanceLev el.Fragment.
      >>
      >The code that I have is very simple.
      >>
      >SimpleClass c = new SimpleClass();
      >>
      >c.Name = "TestName";
      >>
      >XmlSerialize r serializer = new XmlSerializer(t ypeof(SimpleCla ss));
      >>
      >>
      >MemoryStream stream =new MemoryStream();
      >>
      >XmlWriterSetti ngs settings = new XmlWriterSettin gs();
      >>
      >settings.OmitX mlDeclaration = true;
      >>
      >settings.Confo rmanceLevel = ConformanceLeve l.Fragment;
      >>
      >>
      >settings.Encod ing = Encoding.ASCII;
      >>
      >XmlWriter writer = XmlWriter.Creat e(stream,settin gs);
      >>
      >serializer.Ser ialize(writer,c );
      >
      I guess the Serialize method of XmlSerializer calls the WriteStartDocum ent
      method of XmlWriter. Why exactly do you set ConformanceLeve l.Fragment,
      what do you want to achieve with that setting?
      >
      >
      --
      >
      Martin Honnen --- MVP XML
      http://JavaScript.FAQTs.com/

      Comment

      Working...