在文本或字符串中查找xml

时间:2018-02-07 04:44:49

标签: c# xml

如何在字符串或文本中找到xml片段?无论使用什么编程语言,更好的是C#。它可能是非标准的xml文本,字符串/文本可能是这样的:

checkLicense: A Boolean attribute that specifies that a WOPI server SHOULD enforce license restrictions for file types within the ct_app-name block. 


<xs:complexType name="ct_app-name">    
    <xs:sequence> 
        <xs:element name="action" minOccurs="1" maxOccurs="unbounded" type="ct_wopi-action"/>
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="favIconUrl" type="xs:string" use="optional"/>
        <xs:attribute name="checkLicense" type="xs:boolean" use="optional" default="false"/> 
</xs:complexType>


The ct_wopi-action complex type specifies an action (such as viewing a file) that a WOPI client can perform. 

1 个答案:

答案 0 :(得分:0)

这个问题有点模糊(有点动人的目标)

但是,如果你想要<>之间的所有内容,你可以只使用regex贪婪

@"\<.*\>"

实施例

var pattern = @"\<.*\>";
var xml = @"blah blah blah
            dsfsdf,<d [j>ohn]
            ZXZx<John >Johnson
            sdfdsfdsf";
var match = Regex.Match(xml , pattern, RegexOptions.Multiline | RegexOptions.Singleline;

if (match.Success)
{
   Console.WriteLine(match.Groups[0]);
}

但是,如果您想匹配更具体的标签,请使用类似

的模式
@"\<xs:complexType.*\</xs:complexType>"

在这两种情况下,match.Groups[0]都会返回xml,而match.Success会确定您是否有

You can test it here