Home > Software design >  Wrap Xmlnode with <b> tags
Wrap Xmlnode with <b> tags

Time:03-31

I have the following:

            XmlNode catchword = doc.CreateElement("CATCHWORDS");
            XmlNode subjectindex = doc.CreateElement("SUBJECT_INDEX");
            subjectindex.InnerText = DropDownList6.SelectedValue;
            catchword.AppendChild(subjectindex);

This is the output:


    <CATCHWORDS>
      <SUBJECT_INDEX>ADMINISTRATIVE LAW</SUBJECT_INDEX>
    </CATCHWORDS>

But i want to output these instead:


    <CATCHWORDS>
      <b><SUBJECT_INDEX>ADMINISTRATIVE LAW</SUBJECT_INDEX></b>
    </CATCHWORDS>

How i can achieve this?

I try to follow Wrap XmlNode with tags - C#, but did not understand.

Can someone give example based on my code?

CodePudding user response:

You can't just put HTML tags inside XML. If you want to put HTML tags inside XML, you have to encode them as entities.

subjectindex.InnerText = "<b>"
  DropDownList6.SelectedValue   "</b>";

CodePudding user response:

I'd suggest using LINQ to XML instead of XmlDocument - it's a much more modern API, and far nicer to use as a result.

Your current code would be this:

var element = new XElement("CATCHWORDS",
    new XElement("SUBJECT_INDEX", DropDownList6.SelectedValue)
);

And to amend to wrap that in b:

var element = new XElement("CATCHWORDS",
    new XElement("b",
        new XElement("SUBJECT_INDEX", DropDownList6.SelectedValue)
    )
);
  • Related