Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:06:32+00:00 2026-05-14T15:06:32+00:00

Can I use Linq-to-xml to persist my object state without having to use/know Xpath

  • 0

Can I use Linq-to-xml to persist my object state without having to use/know Xpath & XSD Syntax?

ie. really looking for simple but flexible way to persist a graph of object data (e.g. have say 2 or 3 classes with associations) – if Linq-to-xml were as simple as saying “persist this graph to XML”, and then you could also query it via Linq, or load it into memory again/change/then re-save to the xml file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:06:32+00:00Added an answer on May 14, 2026 at 3:06 pm

    You don’t usually need XPath or XSD to use LINQ-to-XML, but it also won’t do what you want. XmlSerializer comes close , but is a tree serializer, not a graph serializer.

    DataContractSerializer (.NET 3.0) does offer graph support via one of the overloaded constructors, but doesn’t offer full control over the xml.

    BinaryFormatter offers graph support and metadata-/type-based workings, but is very brittle if you ever change your assembly, and is not portable between platforms.

    I suspect the thing to figure out is: is my data a tree or a graph? XmlSerializer may already do what you need.


    using System;
    using System.Runtime.Serialization;
    using System.IO;
    [DataContract]
    public class Outer {
        [DataMember]
        public Inner Inner { get; set; }
    }
    [DataContract]
    public class Inner {
        [DataMember]
        public Outer Outer { get; set; }
    }
    class Program {
        static void Main() {
            // make a cyclic graph
            Outer outer = new Outer(), clone;
            outer.Inner = new Inner { Outer = outer };
    
            var dcs = new DataContractSerializer(typeof(Outer), null,
                int.MaxValue, false, true, null);
            using (MemoryStream ms = new MemoryStream()) {
                dcs.WriteObject(ms, outer);
                ms.Position = 0;
                clone = (Outer)dcs.ReadObject(ms);
            }
            Console.WriteLine(ReferenceEquals(
                clone, clone.Inner.Outer)); // true
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use LINQ to achieve the following? I have some c# object
Can we use Business Objects which we made & data acess Layer of LINQ?
So, I can easily use LINQ to XML to traverse a properly set-up XML
Using EF we can use LINQ to read data which is rather simple (especially
I am wondering if you can use Linq to SQL using a .mdf file
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own
I am trying to create Linq Expression that I can use to query child
I can't use TryGetValue() from dictionary in linq expression with anonymous type. Dictionary<string, string>
How can I use table hints in LINQ to SQL when calling submit changes
I asked a question earlier about why left joins in Linq can't use defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.