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 9232861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:24:49+00:00 2026-06-18T06:24:49+00:00

I’m dealing with a number of large descriptive XML files that I only need

  • 0

I’m dealing with a number of large descriptive XML files that I only need to read the outer most elements, change the values and then save it out again.

As an example –

<Model Name="someModel">
    <Material Name="someMaterial" Effect="someEffect">
        <Texture Name="tex1" Path="somePath"/>
        <Colour Name="colour1" Value="FFF"/>
        <Layer Index="0"/>
        ... many more elements I don't are about
    </Material>
</Model>

I want to deserialize the above, change a Material attribute, and then save it back out again. But as I don’t care about Materials elements (just its attributes), I’d rather not have to add them all to the class Im deserialzing too, .. however, I need them to still be written back out when I save.

At the moment, I’m doing this as follows, but I’m wondering if there’s a better way.

namespace WpfApplication9
{
    public class Material
    {
        [XmlAttribute]
        public string Name
        {
            get;
            set;
        }

        [XmlAttribute]
        public string Effect
        {
            get;
            set;
        }
    }

    public class Item
    {
        [XmlAttribute]
        public string Name
        {
            get;
            set;
        }

        public Material Material
        {
            get;
            set;
        }

        public static Item Load(string _path)
        {
            Item item = new Item();
            item.m_doc = new XmlDocument();
            item.m_doc.Load(_path);

            XmlNode rootNode = item.m_doc.FirstChild;

            item.Name = rootNode.Attributes["Name"].InnerText;

            XmlNode materialNode = rootNode.FirstChild;

            item.Material = new Material();
            item.Material.Name = materialNode.Attributes["Name"].InnerText;
            item.Material.Effect = materialNode.Attributes["Effect"].InnerText;

            return item;
        }

        public void Save(string _path)
        {
            XmlNode rootNode = m_doc.FirstChild;

            rootNode.Attributes["Name"].InnerText = Name;

            XmlNode materialNode = rootNode.FirstChild;

            materialNode.Attributes["Name"].InnerText = Material.Name;
            materialNode.Attributes["Effect"].InnerText = Material.Effect;

            m_doc.Save(XmlWriter.Create(_path, new XmlWriterSettings()
            {
                IndentChars = "\t",
                Indent = true
            }));
        }

        XmlDocument m_doc;
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Item item = Item.Load("Data.xml");
            item.Name = "Terry";
            item.Save("Data.xml");
        }
    }
}
  • 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-06-18T06:24:51+00:00Added an answer on June 18, 2026 at 6:24 am

    There is the XmlAnyElementAttribute. It instructs the deserializer to store unrecognized elements as is. However, depending on your case you might be better served using an XmlReader-XmlWriter filter, or an XmlDocument with XPath.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.