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

前端 未结 2 1563
萌比男神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:58

    an issue using the XNode.DeepEquals when there is an empty tags, another way to compare all XML elements from XML documents (this should works even if the XML closing tags are different)

    public bool CompareXml()
    {
            var doc = @"
            
                General
                
                  Aravind Kumar Eriventy
                  
                  9052534488
                
              ";
    
            var doc1 = @"
            
                General
                
                  Aravind Kumar Eriventy
                  
                  9052534488
                
              ";
    
        return XmlDocCompare(XDocument.Parse(doc), XDocument.Parse(doc1));
    
    }
    
    private static bool XmlDocCompare(XDocument doc,XDocument doc1)
    {
        IntroduceClosingBracket(doc.Root);
        IntroduceClosingBracket(doc1.Root);
    
        return XNode.DeepEquals(doc1, doc);
    }
    
    private static void IntroduceClosingBracket(XElement element)
    {
        foreach (var descendant in element.DescendantsAndSelf())
        {
            if (descendant.IsEmpty)
            {
                descendant.SetValue(String.Empty);
            }
        }
    }
    

提交回复
热议问题