People, I need help with this problem.

I have a string like the following.

<JDF ID="n20090409EQ0001" Type="Product" JobID="000004-01" xmlns="http://www.CIP4.org/JDFSchema_1_1" Status="Waiting" Version="1.3" xsi:type="Product" JobPartID="B30" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:scr="htt://new.scr.url/abc" Activation="Active".......

It continues like that. From it, I need to extract ALL the occurrences which start with "xmlns:"

For example, you have these three tags in the above example:

xmlns="http://www.CIP4.org/JDFSchema_1_1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:scr="htt://new.scr.url/abc"

From those, I need to extract the second and third ones, namely,

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

AND

xmlns:scr="htt://new.scr.url/abc"

(I need the full string, including the parts "xmlns:xsi=" , "xmlns:scr", etc, not just the value of those tags)
How can I do that?

Please help ASAP. Thanks in advance.

Recommended Answers

All 7 Replies

thanks for join in this forum
i really happy and gracefull

Use a Regex.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = @"<JDF ID=""n20090409EQ0001"" Type=""Product"" JobID=""000004-01"" xmlns=""http://www.CIP4.org/JDFSchema_1_1"" Status=""Waiting"" Version=""1.3"" xsi:type=""Product"" JobPartID=""B30"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:scr=""htt://new.scr.url/abc"" Activation=""Active"".......";
        string pattern = "xmlns:.*?\".*?\"";

        MatchCollection matches = Regex.Matches(input, pattern);

        foreach (Match match in matches)
            Console.WriteLine(match.Value);

        Console.Read();
    }
}

It prints

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:scr="htt://new.scr.url/abc"
commented: Helped solving the exact problem I had. Great answer! +2

Use a Regex.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = @"<JDF ID=""n20090409EQ0001"" Type=""Product"" JobID=""000004-01"" xmlns=""http://www.CIP4.org/JDFSchema_1_1"" Status=""Waiting"" Version=""1.3"" xsi:type=""Product"" JobPartID=""B30"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:scr=""htt://new.scr.url/abc"" Activation=""Active"".......";
        string pattern = "xmlns:.*?\".*?\"";

        MatchCollection matches = Regex.Matches(input, pattern);

        foreach (Match match in matches)
            Console.WriteLine(match.Value);

        Console.Read();
    }
}

It prints

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:scr="htt://new.scr.url/abc"

Thank you sir, it worked like a charm!
I'll also be grateful if you can explain the syntax of the "pattern".
Thanks again.

as far as i can see....

from this code

string pattern = "xmlns:.*?\".*?\"";

the

xmlns:

says what exactly to look for... must exactly match that...

then moving on to the

.*?\"

the "." i think is some kind of append to the pattern
it then says the next part of the match can be anything (the use of a wildcard [*])

finally moving on to the next part

.*?\"";

once again the "." for append (i think)
again a wild card used (*) but this time the " is added at the end... saying the pattern must end with the quote(")..
finally ending the pattern syntax with the semi-colon(;)

if im wrong do please correct me...
ive never used the above function.. and this is what i can tell from just looking at it

as far as i can see....

from this code

string pattern = "xmlns:.*?\".*?\"";

the

xmlns:

says what exactly to look for... must exactly match that...

then moving on to the

.*?\"

the "." i think is some kind of append to the pattern
it then says the next part of the match can be anything (the use of a wildcard [*])

finally moving on to the next part

.*?\"";

once again the "." for append (i think)
again a wild card used (*) but this time the " is added at the end... saying the pattern must end with the quote(")..
finally ending the pattern syntax with the semi-colon(;)

if im wrong do please correct me...
ive never used the above function.. and this is what i can tell from just looking at it

I found out what it is about, and you're more or less correct.

. represents any character, so .[dot] followed by * means that any number of any characters.

thanx for the reply :D now i know to

The other approach is:

char [] keyChars={'x','m','l','n','s'};
string [] vals;

vals= input.Split(keyChars);

for (int i=0;i<vals.Length;i++)
{
Console.WriteLine("xmlns" + vals);
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.