none
How to add <meta charset="utf-8"> using XMLElement in C#

    Question

  • Hi,

     How do I use XElement to add the above html text in ?

     1) <html lang="en" prefix="op: http://media.facebook.com/op#">

      2)<meta charset="utf-8">

    I am trying to use the below sample code but due to space, I keep getting error. Any method to do it ?

    var xDocument = new XDocument(
                new XDocumentType("html", null, null, null),
                new XElement("html",
                    new XElement("head"),
                    new XElement("body",
                        new XElement("p",
                            "This paragraph contains ", new XElement("b", "bold"), " text."
                        ),
                        new XElement("p",
                            "This paragraph has just plain text."
                        )
                    )
                )
            );
    

    Thursday, April 6, 2017 3:58 AM

All replies

  • Check here how to use LINQ with XML

    MSDN also explain this well here.


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora


    • Edited by RohitArora Thursday, April 6, 2017 4:20 AM
    Thursday, April 6, 2017 4:18 AM
  • Try this too:

    var xDocument = new XDocument(
    		new XDocumentType( "html", null, null, null ),
    			new XElement( "html", new XAttribute( "lang", "en" ), new XAttribute( "prefix", "op:  http://media.facebook.com/op#" ),
    				new XElement( "head",
    					new XElement( "meta", new XAttribute( "charset", "utf-8" ) )
    				),
    				new XElement( "body",
    					new XElement( "p",
    							"This paragraph contains ", new XElement( "b", "bold" ), " text."
    					),
    					new XElement( "p",
    							"This paragraph has just plain text."
    					)
    				)
    			)
    		);
    
    

    Thursday, April 6, 2017 7:08 AM
  • Hi blueberry88,

    Thank you for posting here.

    For your question, you could try the following code.

      static void Main(string[] args)
            {
                CreateXmlFile();
            }
            public static void CreateXmlFile()
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                XmlNode root = xmlDoc.CreateElement("Users");
                xmlDoc.AppendChild(root);
    
                XmlNode node1 = xmlDoc.CreateNode(XmlNodeType.Element, "html", null);  
                root.AppendChild(node1);
                SetAttributeValue(node1, "lang", "en");
                SetAttributeValue(node1, "prefix", "op: http://media.facebook.com/op#");
    
                XmlNode node2 = xmlDoc.CreateNode(XmlNodeType.Element, "meta", null);
                root.AppendChild(node2);
                SetAttributeValue(node2, "charset", "utf-8");
    
                try
                {
                    xmlDoc.Save(@"C:\Users\v-wezan\Desktop\CreateXML.xml");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                //Console.ReadLine();
    
            }
            //public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
            //{
            //    XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            //    node.InnerText = value;
            //    parentNode.AppendChild(node);
            //}
            public static void SetAttributeValue(XmlNode xmlne, string name, string value)
            {
                XmlAttribute node = xmlne.OwnerDocument.CreateAttribute(name);
                xmlne.Attributes.Append(node);
                node.Value = value;
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 6, 2017 7:10 AM
    Moderator
  • Hi,

     I try using the below codes and my stringVariable is actually contents with HTML tag but when I put into XElement , it becomes all escape characters, May I know how do I code it such that the html tag still remain in the XML file>

      new XElement("br", stringVariable)

    stringVariable="<strong>test</strong>

    the xml will display as &lt;address/&gt;

    Friday, April 7, 2017 1:16 AM
  • Hi blueberry88,

    Thank you for feedback.

    I do not have a good understanding about what you really want. Could you provide us more information?

    What xml file you want? Please give us some samples about xml. And if you have some errors in your code, please provide some demo code for us to test.

    We are waiting for your update.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, April 7, 2017 1:59 AM
    Moderator
  • Hi Wendy,

     I am trying to compose the value from my database into an XML syntax.

    I am using the below codes to do and write out to an aspx file .

    I am trying to pull the value from my database and put into the below source code. Look for the varString below.

    the html tag should show as this: =<strong>test</strong> in the xml article node but currently is showing &lt;address/&gt out  when is displayed in aspx file

      var xDocument = new XDocument(
                                     new XDocumentType("html", null, null, null),
                                     new XElement("html",
                                      new XAttribute("lang", "en"), new XAttribute("prefix", "op:http://media.facebook.com/op#"),
                                      new XElement("head",
                                       new XElement("meta", new XAttribute("charset", "utf-8")),
                                       new XElement("link", new XAttribute("rel", "canonical"), new XAttribute("href", domain + Sitecore.Links.LinkManager.GetItemUrl(c2, options))),
                                       new XElement("meta", new XAttribute("property", "op:markup_version"), new XAttribute("content", "v1.0"))
                                       ),
                                       new XElement("body",
                                           new XElement("article",
                                               new XElement("header", c2["Main Title"]),
                                               new XElement("br", varstring)))
                                       )
                                    );

    Friday, April 7, 2017 2:17 AM
  • One of the known solutions is: ‘new XElement( "br", XElement.Parse( varstring ) )’.

    In order to build XML or HTML, you can also use ‘XmlWriter.Create(stringBuilder)’, then call various functions of the writer. The result will be in string builder.

    Friday, April 7, 2017 4:33 AM
  • I will encounter the following error if I use
    new XElement( "br", XElement.Parse( varstring ) )

    Data at the root level is invalid

    Friday, April 7, 2017 4:55 AM
  • Hi blueberry88,

    Thank you for feedback.

    For your question, change the following code in my previous reply.

                node.InnerXml = value;// change node.InnerText = value; to node.InnerXml = value;

    In xml, try the following code.

      static void Main(string[] args)
            {
                CreateXmlFile();
            }
            public static void CreateXmlFile()
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                XmlNode root = xmlDoc.CreateElement("Users");
                xmlDoc.AppendChild(root);
    
                XmlNode node1 = xmlDoc.CreateNode(XmlNodeType.Element, "html", null);  
                root.AppendChild(node1);
                SetAttributeValue(node1, "lang", "en");
                SetAttributeValue(node1, "prefix", "op: http://media.facebook.com/op#");
    
                XmlNode node2 = xmlDoc.CreateNode(XmlNodeType.Element, "meta", null);
                root.AppendChild(node2);
                SetAttributeValue(node2, "charset", "utf-8");
    
                string varstring = "<strong > test</strong>";
                XmlNode result = xmlDoc.CreateNode(XmlNodeType.Element, "result", null);
                CreateNode(xmlDoc, result, "br", varstring);        
                root.AppendChild(result);
    
                try
                {
                    xmlDoc.Save(@"C:\Users\v-wezan\Desktop\CreateXML.xml");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                //Console.ReadLine();
    
            }
            public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
            {
                XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
                node.InnerXml = value;
                //node.InnerText = value;
                parentNode.AppendChild(node);
            }
            public static void SetAttributeValue(XmlNode xmlne, string name, string value)
            {
                XmlAttribute node = xmlne.OwnerDocument.CreateAttribute(name);
                xmlne.Attributes.Append(node);
                node.Value = value;
            }     

    For ASP.NET, please post a new thread in ASP.NET forum.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, April 7, 2017 5:40 AM
    Moderator
  • I will encounter the following error if I use
    new XElement( "br", XElement.Parse( varstring ) )

    Data at the root level is invalid

    I think that this means that varstring does not correspond to previous samples, i.e. it is not an XML like “<strong>test</strong>”. It is also not clear if the problem was solved or not.



    • Edited by Viorel_MVP Friday, April 7, 2017 6:42 AM
    Friday, April 7, 2017 6:42 AM
  • I got this error:

    Unexpected end of file has occurred. The following elements are not closed: br, br, br, br, br, br, br, br, br, br, br, img, br, br, br, br, br, br, br, br, br, br, br.

    Sorry I am not too familiar with the xml syntax

    my varstring value will have the below content:

    The world stage has been rather <strong>chaotic of late</strong>, to say the least.<br>
    <br>
    <strong>The most dramatic happenings of the week include attacks at reputable five star hotels in India as well as anti-government protesters staking out Thailand airports, making flights in and out of Bangkok impossible. </strong>
    <br>
    <br>
    <table width="220" cellspacing="7" border="0" align="left" style="border:0pt solid #ffffff;border-image: initial; height: 200px;">
        <tbody>
            <tr>
                <td>
                <div style="text-align: left;"> </div>


    • Edited by blueberry88 Friday, April 7, 2017 7:15 AM rework
    Friday, April 7, 2017 7:03 AM