使用C#从字符串读取自定义标签值

时间:2018-10-18 12:43:55

标签: c# asp.net

我想使用c#捕获以星号标记的信息,它包含用于启动和关闭c标签的行标签,但是模式不同,例如在某处为tag和某处为tag的情况下获取值。

<row r="186">
    <c t="inlineStr" r="A186"><is><t>**DNC**</t></is></c>
    <c t="n" r="B186"><v>**10147**</v></c>
    <c t="n" r="C186"><v>**100**</v></c>
    <c t="n" r="D186"><v>**-4.00**</v></c>
    <c t="inlineStr" r="E186"><is><t>**Y4**</t></is></c>
    <c t="n" r="F186"><v>**1**</v></c>
    <c t="n" r="G186"><v>**9193393593**</v></c>
    <c t="inlineStr" r="H186"><is><t>**MR**</t></is></c>
    <c t="inlineStr" r="I186"><is><t>**Bradley**</t></is></c>
    <c t="inlineStr" r="J186"><is><t/></is></c>
    <c t="inlineStr" r="K186"><is><t>**Goss**</t></is></c>
    <c t="inlineStr" r="L186"><is><t>**1781 hwy15**</t></is></c>
    <c t="inlineStr" r="M186"><is><t/></is></c>
    <c t="inlineStr" r="N186"><is><t/></is></c>
    <c t="inlineStr" r="O186"><is><t>**Creedmoor**</t></is></c>
    <c t="inlineStr" r="P186"><is><t>**NC**</t></is></c>
    <c t="inlineStr" r="Q186"><is><t/></is></c>
    <c t="n" r="R186"><v>27522</v></c>
    <c t="inlineStr" r="S186"><is><t>**USA**</t></is></c>
    <c t="inlineStr" r="T186"><is><t>**M**</t></is></c>
    <c t="inlineStr" r="U186"><is><t/></is></c>
    <c t="inlineStr" r="V186"><is><t/></is></c>
    <c t="inlineStr" r="W186"><is><t/></is></c>
    <c t="inlineStr" r="X186"><is><t>**2014-11-19 18:53:43:Viagra 30  Pastillas 30pills 1 44.95**</t></is></c>
    <c t="n" r="Y186"><v>**35**</v></c>
    <c t="inlineStr" r="Z186"><is><t>**2016-08-13 18:07:17**</t></is></c>
    </row>

赞:

for each row_tag
{
console.writline(c_tagvalue1);
console.writline(c_tagvalue2);
console.writline(c_tagvalue3);
console.writline(c_tagvalue4);
.... upto 26 or count of c tag
}

3 个答案:

答案 0 :(得分:1)

您可以像下面这样使用Linq To XML:

curl -s -X POST -H "Content-Type: application/json" \
    --data "{
  'q': 'The Great Pyramid of Giza (also known as the Pyramid of Khufu or the
        Pyramid of Cheops) is the oldest and largest of the three pyramids in
        the Giza pyramid complex.',
  'source': 'en',
  'target': 'es',
  'format': 'text'
}" "https://translation.googleapis.com/language/translate/v2?key=YOURVALIDAPIKEYHERE"

具有多行:

void Main()
{
    string xml = @"<row r=""186"">
        <c t=""inlineStr"" r=""A186""><is><t>**DNC**</t></is></c>
        <c t=""n"" r=""B186""><v>**10147**</v></c>
        <c t=""n"" r=""C186""><v>**100**</v></c>
        <c t=""n"" r=""D186""><v>**-4.00**</v></c>
        <c t=""inlineStr"" r=""E186""><is><t>**Y4**</t></is></c>
        <c t=""n"" r=""F186""><v>**1**</v></c>
        <c t=""n"" r=""G186""><v>**9193393593**</v></c>
        <c t=""inlineStr"" r=""H186""><is><t>**MR**</t></is></c>
        <c t=""inlineStr"" r=""I186""><is><t>**Bradley**</t></is></c>
        <c t=""inlineStr"" r=""J186""><is><t/></is></c>
        <c t=""inlineStr"" r=""K186""><is><t>**Goss**</t></is></c>
        <c t=""inlineStr"" r=""L186""><is><t>**1781 hwy15**</t></is></c>
        <c t=""inlineStr"" r=""M186""><is><t/></is></c>
        <c t=""inlineStr"" r=""N186""><is><t/></is></c>
        <c t=""inlineStr"" r=""O186""><is><t>**Creedmoor**</t></is></c>
        <c t=""inlineStr"" r=""P186""><is><t>**NC**</t></is></c>
        <c t=""inlineStr"" r=""Q186""><is><t/></is></c>
        <c t=""n"" r=""R186""><v>27522</v></c>
        <c t=""inlineStr"" r=""S186""><is><t>**USA**</t></is></c>
        <c t=""inlineStr"" r=""T186""><is><t>**M**</t></is></c>
        <c t=""inlineStr"" r=""U186""><is><t/></is></c>
        <c t=""inlineStr"" r=""V186""><is><t/></is></c>
        <c t=""inlineStr"" r=""W186""><is><t/></is></c>
        <c t=""inlineStr"" r=""X186""><is><t>**2014-11-19 18:53:43:Viagra 30  Pastillas 30pills 1 44.95**</t></is></c>
        <c t=""n"" r=""Y186""><v>**35**</v></c>
        <c t=""inlineStr"" r=""Z186""><is><t>**2016-08-13 18:07:17**</t></is></c>
        </row>";

    var result = from e in XElement.Parse(xml).DescendantsAndSelf("c")
                 select new {
                    t = (string)e.Attribute("t"),
                    r = (string)e.Attribute("r"),
                    isT = (string)e.Element("is")?.Element("t"),
                    v = (string)e.Element("v")
                 };

    foreach (var e in result)
    {
        Console.WriteLine($"t:{e.t}, r:{e.r}, isT:{e.isT}, v:{e.v}");
    }
}

答案 1 :(得分:0)

您可以使用正则表达式:

MatchCollection mc = Regex.Matches(input, @"<c[^>]+>.*?>([a-zA-Z-0-9.*: ]+)<.*?<\/[^>]+>$");
foreach(Match m in mc)
   Console.WriteLine(m.Groups[1].Value);

Live Example of Matches

答案 2 :(得分:-1)

给出示例,前提是您可以进行正则表达式匹配

 var result = from Match match in Regex.Matches(xmlString, @"\*\**.*?\*\*") 
        select match.ToString().Replace("**", "");