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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:51:06+00:00 2026-06-08T18:51:06+00:00

I have an XML doc. For example this: <Root xmlns:x=anynamespace xmlns:html=htmlnamespace> <x:Data>bla bla</x:Data> </Root>

  • 0

I have an XML doc. For example this:

<Root xmlns:x="anynamespace" xmlns:html="htmlnamespace">
   <x:Data>bla bla</x:Data>
</Root>

Here I got html namespace to format data. BUT value of element can be e.g.
<html:Font ...>bla bla</html:Font> or
bla <html:Font ...>bla</htmk:Font>

In my C# code I do this:
new XElement(main + "Data",myvalue); //main is namespace
As a result I got <x:Data>&lt;html:Font ...&gt;bla bla etc. Linq replaced key tags with their text codes. So this is unacceptable.

Then i tried this:new XElement(main + "Data",XElement.Parse(myvalue));
There I got exception that prefix html didnt recognized.

Does anyone faced such problem? How did you solve that?

  • 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-08T18:51:07+00:00Added an answer on June 8, 2026 at 6:51 pm

    Usually you would not construct the contents from a string but rather simply construct the nodes using LINQ to XML e.g.

                XElement foo = XElement.Parse(@"<foo xmlns=""http://example.com/ns1"" xmlns:html=""http://example.com/html"">
      <bar>bar 1</bar>
    </foo>");
                foo.Add(new XElement(foo.GetNamespaceOfPrefix("html") + "p", "Test"));
    
                Console.WriteLine(foo);
    

    creates the XML

    <foo xmlns="http://example.com/ns1" xmlns:html="http://example.com/html">
      <bar>bar 1</bar>
      <html:p>Test</html:p>
    </foo>
    

    If you want to parse a fragment given as a string then perhaps the following approach helps:

            public static void AddWithContext(this XElement element, string fragment)
            {
                XmlNameTable nt = new NameTable();
                XmlNamespaceManager mgr = new XmlNamespaceManager(nt);
    
                IDictionary<string, string> inScopeNamespaces = element.CreateNavigator().GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);
    
                foreach (string prefix in inScopeNamespaces.Keys)
                {
                    mgr.AddNamespace(prefix, inScopeNamespaces[prefix]);
                }
    
                using (XmlWriter xw = element.CreateWriter())
                {
                    using (StringReader sr = new StringReader(fragment))
                    {
                        using (XmlReader xr = XmlReader.Create(sr, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment }, new XmlParserContext(nt, mgr, xw.XmlLang, xw.XmlSpace)))
                        {
                            xw.WriteNode(xr, false);
                        }
                    }
                    xw.Close();
                }
            }
        }
    
        class Program
        {
            static void Main()
            {
                XElement foo = XElement.Parse(@"<foo xmlns=""http://example.com/ns1"" xmlns:html=""http://example.com/html"">
      <bar>bar 1</bar>
    </foo>");
                foo.Add(new XElement(foo.GetNamespaceOfPrefix("html") + "p", "Test"));
    
                Console.WriteLine(foo);
                Console.WriteLine();
    
                foo.AddWithContext("<html:p>Test 2.</html:p><bar>bar 2</bar><html:b>Test 3.</html:b>");
    
                foo.Save(Console.Out, SaveOptions.OmitDuplicateNamespaces);
    
            }
    

    That way I get

    <foo xmlns="http://example.com/ns1" xmlns:html="http://example.com/html">
      <bar>bar 1</bar>
      <html:p>Test</html:p>
      <html:p>Test 2.</html:p>
      <bar>bar 2</bar>
      <html:b>Test 3.</html:b>
    </foo>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, here's the question, I have an xml doc that is being import into
I Have XML similar to this(omitted parts for brevity): <uformrecord> <state>Submitted</state> <created>2012-06-19T11:31:54</created> <updated>2012-06-19T11:32:13</updated> <id>53225sas3c1-d727-42cd-93a6-97cd778e5ee9</id>
I have xml like this: <rule> <word>I</word> <word>need</word> <word>more</word> <marker> <word>money</word> </marker> <word>now</word> </rule>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have xml like this: <configurationData> <path name='b'> <path name='a'> <setting name='s1'> ![CDATA[XXXX]] </setting>
Let's say we have the following XML document: <root> <options> ... </options> <children> <child
I'm practicing some XSL and using this XML document as a simple example: <?xml
I have an existing data model. I would like to express this data model
I have an XML file formatted like this: <?xml version=1.0 encoding=utf-8?> <Snippets> <Snippet name=abc>
I have code which parses XML that looks like this: <custom_fields> <custom_field> <column_name>foo</column_name> <column_value>0</column_value>

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.