CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    Question Serialize to XML using XSD

    I'm making XML files from Entity Framework objects using this code:
    Code:
               XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.Encoding = utf8noBOM; 
                try
                {
                    var rows = ef.message.Where(r => r.Rec_Status == 0); 
                    foreach (var row in rows)
                    {
                        filename = System.IO.Path.Combine(ConfigurationManager.AppSettings["XMLLogDirIncoming"].ToString(), row.REC_ID.ToString() + ".xml");
                        using (XmlWriter writer = XmlWriter.Create(filename, settings))
                        {
                            DataContractSerializer serializer = new DataContractSerializer(row.GetType());
                            serializer.WriteObject(writer, row);
                        }
                        files.Add(filename);
                    }
                }
                catch (Exception ex)
    But doing this, I get a lot of "crap" from EF which I don't need. Rather, I'd like the XML to conform to an XSD to reach a certain format. How do I do that? Can I specify an XSD for the serializer, or am I forced to transform my data afterwards?

    Thanks in advance :-)

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Serialize to XML using XSD

    Select a new anonymous object with the fields you need.

  3. #3
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    Re: Serialize to XML using XSD

    The problem is, that my request comes from a gazillion tables (and there might be more in the future. But all have an EntityKey field which I don't need etc.
    The things i _need_ are always different from table to table, while the things I _don't_ need are always the same.
    I tried to experiment a bit with XML transformation, but with little luck.
    I fear, that the only solution is something like the one you describe, but that will require code if or when a new table is added to the "collection".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured