Read xprocessinginstruction using linq to xml

I have this much:

                        XDocument doc = XDocument.Load(existFile);

                        if (doc.FirstNode is XProcessingInstruction)
                            XProcessingInstruction PI = (XProcessingInstruction)doc.FirstNode;

And, ?PI.Data yields:

"format=\"850\" sender=\"AMAZON\" receiver=\"MYCLIENT\" ctrl=\"1\""

How do I read each of the individual attributes (format,sender,receiver,ctrl)?  Is there a method on PI.Data that will do this?  I haven't found one yet.
Who is Participating?
zc2Connect With a Mentor Commented:
Probably there's no standard XML methods. Only an XElement can give access to its attributes, but a PI is not derived from XElement.

I strongly recommend you to get yourself familiar with the regular expressions. It's a very powerful technique to handle string data, could by used everywhere.
You can try to use a regular expression:
                MatchCollection mm = Regex.Matches(PI.Data, @"(\w+)=\\""(.+?)\\""");
                foreach( Match m in mm )
                    Console.WriteLine( "Found {0} = {1}", m.Groups[1].Value, m.Groups[2].Value );

Open in new window

g_johnsonAuthor Commented:
I don't know much about Regex, but using the above mm returns nothing (the foreach is skipped); mm.Count() returns 0.

But, if what you are saying is that you just use string handling techniques, I can figure something else out.  I'm just wondering if there is some System.Linq.XML standard method to use to parse it out.
g_johnsonAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.