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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:03:06+00:00 2026-05-21T05:03:06+00:00

Suppose, I have a simple type: public class Report { public Report() { BirthDate

  • 0

Suppose, I have a simple type:

public class Report
{
    public Report()
    {
        BirthDate = new Element();
        BirthPlace = new Element();
    }

    public Element BirthDate { get; set; }
    public Element BirthPlace { get; set; }
}

public class Element
{
    [XmlAttribute("published")]
    public bool Published { get; set; }
    [XmlText]
    public string Value { get; set; }
}

I defined simple extension method for serialization purposes:

public static class TheHelper
{
    public static string Serialize<T>(this T source, Encoding encoding)
    {
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(typeof(T));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, encoding);

        xs.Serialize(xmlTextWriter, source);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        return encoding.GetString(memoryStream.ToArray());
    }
}

When sample Report object is created, it is next serialized to xml format:

Report r = new Report();
r.BirthDate.Published = true;
r.BirthDate.Value = DateTime.Now.AddYears(-1000).ToString("yyyy-MM-dd");
r.BirthPlace.Published = false;
r.BirthPlace.Value = "K-PAX";

string xml = r.Serialize(Encoding.UTF8);

Output document shown below is created:

<?xml version="1.0" encoding="utf-8"?>
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <BirthDate published="true">1011-04-07</BirthDate>
    <BirthPlace published="false">K-PAX</BirthPlace>
</Report>

But I would like to add language identification using special attribute named xml:lang:

<?xml version="1.0" encoding="utf-8"?>
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <BirthDate published="true" xml:lang="en-GB">1011-04-07</BirthDate>
    <BirthDate published="true" xml:lang="kp-AX">07.04.1011</BirthDate>
    <BirthPlace published="false" xml:lang="en-GB">K-PAX</BirthPlace>
    <BirthPlace published="false" xml:lang="kp-AX">k_p4x</BirthPlace>
</Report>

What can be the smart way to achieve this ? I have resources defined for en-GB and kp-AX.. languages. How to modify and create Report object to have multiple tags with different xml-lang attributes to be serializable using XmlSerializer ?

Regards.

  • 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-21T05:03:07+00:00Added an answer on May 21, 2026 at 5:03 am

    This should work:

    public class Element
    {
        [XmlAttribute("published")]
        public bool Published { get; set; }
        [XmlAttribute("xml:lang", DataType = "language")]
        public string Language { get; set; }
        [XmlText]
        public string Value { get; set; }
    }
    

    If you wanted to use the CultureInfo class, you could create a property and use the XmlIgnore attribute on the CultureInfo property and have an addition string property that converts it, like in this LocalizableString example:

    /// <summary>
    /// The language of the <see cref="Value"/>
    /// </summary>
    [XmlIgnore]
    public CultureInfo Language { get; set; }
    
    /// <summary>Used for XML serialization.</summary>
    /// <seealso cref="Language"/>
    [XmlAttribute("xml:lang", DataType = "language")]
    public string LanguageString
    {
        get { return (Language == null || string.IsNullOrEmpty(Language.ToString())) ?
                 null : Language.ToString(); }
        set { Language = string.IsNullOrEmpty(value) ?
                 CultureInfo.InvariantCulture : new CultureInfo(value); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a simple XHTML document that uses a custom namespace for attributes:
I have a simple question. Is there a way ( using reflections I suppose
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have an interface for a service: public interface IFooService { void DoSomething();
Suppose I have an autolocker class which looks something like this: template <T> class
Suppose I want to create a set of observers based on type. That is
I have the following problem: Suppose I have some basic counter class Counter .
Suppose I have a custom collection class that provides some internal thread synchronization. For
I have a very simple class : class Trace { void WriteTrace() { Console.WriteLine(Trace

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.