@@ -747,6 +747,19 @@ private static bool CompareElements(
747747 return false ;
748748 }
749749
750+ private static string AttributeToXpathElement ( XAttribute attr )
751+ {
752+ string attrValue = attr . Value . Replace ( "\" " , """ ) ;
753+ if ( attrValue . Contains ( "'" ) )
754+ {
755+ return $ "[@{ attr . Name . LocalName } =\" { attrValue } \" ]";
756+ }
757+ else
758+ {
759+ return $ "[@{ attr . Name . LocalName } ='{ attrValue } ']";
760+ }
761+ }
762+
750763 private static ( string step , string patchForParent ) GetElementPathStep (
751764 XElement element ,
752765 XElement parent ,
@@ -763,6 +776,10 @@ private static (string step, string patchForParent) GetElementPathStep(
763776 if ( patchForParent == "" )
764777 patchForParent = $ "{ element . Name . LocalName } ";
765778 List < XAttribute > attributes = [ ] ;
779+ if ( element . FirstAttribute != null )
780+ {
781+ patchForParent += AttributeToXpathElement ( element . FirstAttribute ) ;
782+ }
766783 IEnumerable < XElement > matches = parent . XPathSelectElements ( patchForParent ) ;
767784 if ( matches . Count ( ) == 1 && matches . First ( ) == element )
768785 {
@@ -774,20 +791,12 @@ private static (string step, string patchForParent) GetElementPathStep(
774791 }
775792 if ( attributes . Count > 0 )
776793 {
794+ string xpath = $ "{ patchForParent } ";
777795 if ( pathOptions . UseAllAttributes )
778796 {
779- string xpath = $ "{ patchForParent } ";
780797 foreach ( var attr in attributes )
781798 {
782- string attrValue = attr . Value . Replace ( "\" " , """ ) ;
783- if ( attrValue . Contains ( "'" ) )
784- {
785- xpath += $ "[@{ attr . Name . LocalName } =\" { attrValue } \" ]";
786- }
787- else
788- {
789- xpath += $ "[@{ attr . Name . LocalName } ='{ attrValue } ']";
790- }
799+ xpath += AttributeToXpathElement ( attr ) ;
791800 }
792801 matches = parent . XPathSelectElements ( xpath ) ;
793802 if ( matches . Count ( ) == 1 && matches . First ( ) == element )
@@ -801,18 +810,11 @@ private static (string step, string patchForParent) GetElementPathStep(
801810 }
802811 }
803812 }
813+ xpath = $ "{ patchForParent } ";
804814 foreach ( var attr in attributes )
805815 {
806816 string attrValue = attr . Value . Replace ( "\" " , """ ) ;
807- string xpath = $ "{ patchForParent } ";
808- if ( attrValue . Contains ( "'" ) )
809- {
810- xpath += $ "[@{ attr . Name . LocalName } =\" { attrValue } \" ]";
811- }
812- else
813- {
814- xpath += $ "[@{ attr . Name . LocalName } ='{ attrValue } ']";
815- }
817+ xpath += AttributeToXpathElement ( attr ) ;
816818 matches = parent . XPathSelectElements ( xpath ) ;
817819 if ( matches . Count ( ) == 1 && matches . First ( ) == element )
818820 {
0 commit comments