簡體   English   中英

使用C#從字符串讀取自定義標簽值

[英]read custom tag values from string using c#

我想使用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
}

您可以像下面這樣使用Linq To XML:

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}");
    }
}

有多行:

void Main()
{
    string xml = @"<root>
    <row r=""187"">
        <c t=""inlineStr"" r=""A187""><is><t>**New DNC**</t></is></c>
        <c t=""n"" r=""B187""><v>**new v**</v></c>
        </row>
    <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>
        </root>";

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

    // using the awesome LinqPad tool
    // result.Dump();        

foreach (var e in result)
{
    Console.WriteLine("Row:{0}",e.row);
    foreach (var c in e.members)
    {
        Console.WriteLine("\tt:{0}, r:{1}, isT:{2}, v:{3}", 
          c.t, c.r, c.isT, c.v);
    }
}
}

您可以使用正則表達式:

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

現場比賽示例

給定樣本,您可以進行正則表達式匹配

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM