Forcing XDocument.ToString() to include the closing tag when there is no data

前端 未结 2 1583
萌比男神i
萌比男神i 2021-01-11 11:22

I have a XDocument that looks like this:

 XDocument outputDocument = new XDocument(
                new XElement(\"Document\",
                    new XEleme         


        
2条回答
  •  天命终不由人
    2021-01-11 11:41

    Set the Value property of each empty XElement specifically to an empty string.

        // Note: This will mutate the specified document.
        private static void ForceTags(XDocument document)
        {
            foreach (XElement childElement in
                from x in document.DescendantNodes().OfType()
                where x.IsEmpty
                select x)
            {
                childElement.Value = string.Empty;
            }
        }
    

提交回复
热议问题