I have an XElement which it’s output is
<Email>address@email.com</Email>
. Base on some criteria I will possibly need to remove the email address and set it to null. I know I can yous set element.Value =””; but that will not do what I want. I want to modify it so the output will become:
<Email xsi:nil=\"true\" />
I don’t want to create a brand new node because this is a referenced node within a document. and i want to keep the node where it is within the document.
I tried
emailItem.Add(new XAttribute("xsi:nil", "true"));
but I received the following exception
The ‘:’ character, hexadecimal value 0x3A, cannot be included in a
name.
The following changes create the node almost correctly:
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
emailItem.Add(new XAttribute(xsi + "nil", true));
emailItem.Value =""; //How do I set to Null?
I end up with <Email xsi:nil="true"></Email> instead <Email xsi:nil="true"/>
Yes, you need to specify the
XNamedifferently; you can’t just create anXNamein a namespace like that.I suspect you want this:
Complete example:
Output: