0

I want to trim a String with more than 1 character. The String:

<incident_response>
<msg_id>50889_360</msg_id>
<inquiry_id>INC000000087930</inquiry_id>
<response_dt>02.26.2013 14:49:12</response_dt>
<status>0</status>
<error></error>
</incident_response>

Yes, I know, it is a XML File.

string responseXML;
string inc_number = 
responseXML.Trim().Trim('<inquiry_id>').TrimEnd('</inquiry_id>');

I got a CS1012 error, because the are more than one char. I need the whole inquiry_id (INC000000087930) without any other chars. Is there another option to get the number? I can't use the xmlready or something like this, because I get the XML as a string from an API module.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
mnlfischer
  • 397
  • 4
  • 12
  • 26

5 Answers5

10

Having the XML as a string doesn't prevent you from using any of the XML APIs. The easiest to use is the XLinq API. It should be available in .NET 3.5, too.

XDocument doc = XDocument.Parse(xml);
string inquiryId = (string)doc.Root.Element("inquiry_id");
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Actually removing the string directly should be faster because of the fact that XDocument parses the xml first. Or i'm wrong? – Felix K. Feb 26 '13 at 16:05
  • @FelixK.: You could be right, but why care? This most likely isn't a performance bottleneck and the code using a XML parser is more robust and expressive. – Daniel Hilgarth Feb 26 '13 at 16:07
  • You are right. But when you are just writing "This is wrong, use this." the op doesn't know how to use the methods correctly. :-) – Felix K. Feb 26 '13 at 16:09
  • okay, I try it. He couldnt find the namespace for xdocument. I try to use the namespace (http://msdn.microsoft.com/de-de/library/system.xml.linq.xdocument(v=vs.90).aspx), but also he cant find the library linq. – mnlfischer Feb 26 '13 at 16:21
  • @ManuelFischer: Did you add a reference to System.Xml.Linq to your project? – Daniel Hilgarth Feb 26 '13 at 16:22
  • dont think so, I try to implement it. Its possible in vs05? – mnlfischer Feb 26 '13 at 16:27
  • @ManuelFischer: It should be possible, because you are using .NET 3.5 IIRC. – Daniel Hilgarth Feb 26 '13 at 16:28
  • cant find the library linq. Its right so search in the objektbrowser? – mnlfischer Feb 26 '13 at 16:43
  • @ManuelFischer. No, that's not correct. You have to do it like so: Right-click on your project -> Add Reference -> .NET -> Select "System.Xml.Linq" – Daniel Hilgarth Feb 26 '13 at 16:50
0

If you still want to work with strings something like this might work

 responseXML.Substring(responseXML.IndexOf("<inquiry_id>"), responseXML.IndexOf("</inquiry_id>"))

But using an XML API in your case is a cleaner approach.

Guish
  • 4,968
  • 1
  • 37
  • 39
0

If you want only the inquiry _id you could use an Regular Expressions.

string your_xml = @"<incident_response>
<msg_id>50889_360</msg_id>
<inquiry_id>INC000000087930</inquiry_id>
<response_dt>02.26.2013 14:49:12</response_dt>
<status>0</status>
<error></error>
</incident_response>";


Regex re = new Regex(@"<inquiry_id>(?'inquiry_id'.*?)</inquiry_id>");
MatchCollection mc = re.Matches(your_xml);
foreach (Match m in mc)
{
    MessageBox.Show(m.Groups["inquiry_id"].Value);
}
netblognet
  • 1,951
  • 2
  • 20
  • 46
-1

C# supports stringVariable.Replace(string, string), which will replace all occurrences of the first string with the second.

string responseXML;
string inc_number = 
    responseXML.Replace("<inquiry_id>", string.Empty).Replace("</inquiry_id>", string.Empty);

But you will still need to deal with the rest of the XML. An XML parser is better-suited to your needs I think.

-1

try this:

string str = "ggAAAff";
string prefix = "gg";
string suffix = "ff";
if (str.EndsWith(suffix)) str = str.Substring(0, str.Length - suffix.Length);
if (str.StartsWith(prefix)) str = str.Substring(prefix.Length);
taminov
  • 591
  • 7
  • 23