1

I have following values:

<H2:H2LabelComboBoxCell ID="LabelComboboxCellProject"  SecurityID="CD4D3959-0ADB-4375-8DCF-917157528BDE" MaxLength=100/>
<H2:H2LabelFileListCell ID="LabelFileListCellApplicationAttachched" ColumnSpan="1" DataSource="ClientFileH2BindingSource" />
<H2:H2LabelTextBoxCell LabelText="Budget (€)" ColumnSpan="1" ID="LabelTextboxCellBudget" />
<H2W:H2CheckBoxList ID= "H2CheckBoxListReasonForRejection" DataIsDefaultMember="IsDefault" runat="server" />

I need to get value of ID in each row.

I thought of use substring as follows:

string line= @"<H2:H2LabelComboBoxCell ID="LabelComboboxCellProject"  SecurityID="CD4D3959-0ADB-4375-8DCF-917157528BDE"/>"
string id= line.Substring(line.IndexOf("ID="),xyz);

How can i get the value of ID ( value i can use for xyz)

EDIT : I tried this with XmlDocument as Foolows:

string text= " <H2H2LabelTextBoxCell ID="H2LabelTextBoxCell1" runat="server" TextSize=400 PlaceHolder="" LabelText="Translate into" Token="Translation_Into_For_Language_management_Initial" DataSource="SelectedTokensForStaticListTypeBindingSource2" DataMember="Value" Disabled="False" SecurityID="755C974E-211A-4832-B5E2-F3765194E6BC"></H2H2LabelTextBoxCell> "
 XmlDocument doc = new XmlDocument();
 doc.LoadXml(text);

this throws following exception : '400' is an unexpected token. The expected token is '"' or '''. Line 1, position 72.

Due to formatting issues i move back to normal line reading option.

DevT
  • 4,843
  • 16
  • 59
  • 92
  • 1
    This looks like XML - wouldn't it be easier to treat it as such? – germi Nov 23 '15 at 13:05
  • @germi can't treat it like XML, because this value contain maxLength=100 sort of values – DevT Nov 23 '15 at 13:09
  • why do you want to knwo the ID's, and is the code yours? And 100 maxlenght of value is oke to treat as xml – lordkain Nov 23 '15 at 13:09
  • @lordkain i tried with `XmlDocument` but it says can't load file because it contain numeric values. nned to use either " " or ' ' – DevT Nov 23 '15 at 13:13
  • 1
    Cant you just extract it with regex? – Giorgi Nakeuri Nov 23 '15 at 13:16
  • you can use the HTML Agility pack for html.http://stackoverflow.com/questions/846994/how-to-use-html-agility-pack Sounds better for youre problem tyhen working with substring – lordkain Nov 23 '15 at 13:16
  • This example gives what you want http://stackoverflow.com/questions/2462552/get-all-attribute-values-of-given-tag-with-html-agility-pack – lordkain Nov 23 '15 at 13:18
  • Hi, for html documents, you could follow this, http://stackoverflow.com/questions/2253947/how-can-i-get-values-from-html-tags – Shanthini Nov 23 '15 at 13:20

3 Answers3

2

Incase you don't want to do the whole XML thing.

private static int Main(string[] args)
{
    string line= "<H2:H2LabelComboBoxCell ID=\"LabelComboboxCellProject\"  SecurityID=\"CD4D3959-0ADB-4375-8DCF-917157528BDE\"/>";

    string stringToFind = "ID=\"";
    int firstQuote = line.IndexOf(stringToFind) + stringToFind.Length;
    int nextQuote  = line.IndexOf("\"",firstQuote);

    string id= line.Substring(firstQuote,nextQuote-firstQuote);

    System.Console.Write("id="+id);
    return 0;
}
Nik Bo
  • 1,410
  • 2
  • 17
  • 29
MyDeveloperDay
  • 2,485
  • 1
  • 17
  • 20
1

What about:

int start, end;
start = line.IndexOf("ID=\"") + ("ID=\"").Length;
end = line.IndexOf("\"", start);
string id= line.Substring(start,end-start);
Nik Bo
  • 1,410
  • 2
  • 17
  • 29
0

You can also using Split() as well.

Tony Wu
  • 1,040
  • 18
  • 28