• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

Double Quotes in C#

All,

I have a quesion that is just buggin me because I can't think of a way around it. It someone were to pass a XML string value to me and that string value included a node attribute which is typically enclosed in quotes. How could I take that value and do something like a SelectSingleNode with it? The string value appears to always have the "\"" values around the attribute.

For instance, if I have a node:

/Family/Person ID="Son"/BirthDate

If I have the string is defined like:

string strTest = Q"/Family/Person ID=""Son""/BirthDate";

What I end up with is:

"/Family/Person ID=\"Son\"/BirthDate"

If I want to do something like a SelectSingleNode with that value it will not work.

The strTest.Replace("\"","") didn't work.

How would/code I get that to be:

"/Family/Person ID="Son"/BirthDate"

Any info would be greatly appreciated.

Thanks!
0
davism
Asked:
davism
  • 5
  • 4
  • 3
2 Solutions
 
wdosanjosCommented:
Can you provide a sample XML that illustrates your issue?
0
 
wdosanjosCommented:
Have you tried using single quotes as below?

string strTest = "/Family/Person ID='Son'/BirthDate";
0
 
davismAuthor Commented:
Something like this:

<Family>
   <Parents>
     <FatherFirstName>Test</FatherFirstName>
     <FatherLastName>Test2</FatherLastName>
     <MotherFirstName>TestMother</MotherFirstName>
     <MotherLastName>Smith</MotherLastName>
     <Person ID="Son">
        <FirstName>Child1First</FirstName>
        <LastName>Test2</LastName>
        <BirthCountry>US</BirthCountry>
        <BirthCity>SpringField</BirthCity>
        <BirthDate>12/15/1980</BirthDate>
     </Person>
     <Person ID="Daughter">
        <FirstName>Daughter1First</FirstName>
        <LastName>DaughterTest2</LastName>
        <BirthCountry>US</BirthCountry>
        <BirthCity>Denver</BirthCity>
        <BirthDate>4/27/1969</BirthDate>
     </Person>
   </Parents>
</Family>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
davismAuthor Commented:
wdosanjos, I know the sample doesn't exactly fit the string. The string was a sample as well and I obviously missed the "Parents".

I tried that with the single quote but sure is XML is needed the precise aspects i.e. (") vs. (").

But the mere fact is that it will be provided with the double quote I am sure of.
0
 
wdosanjosCommented:
This worked for me.

string xmlstring = "<Family>\n   <Parents>\n     <FatherFirstName>Test</FatherFirstName>\n     <FatherLastName>Test2</FatherLastName>\n     <MotherFirstName>TestMother</MotherFirstName>\n     <MotherLastName>Smith</MotherLastName>\n     <Person ID=\"Son\">\n        <FirstName>Child1First</FirstName>\n        <LastName>Test2</LastName>\n        <BirthCountry>US</BirthCountry>\n        <BirthCity>SpringField</BirthCity>\n        <BirthDate>12/15/1980</BirthDate>\n     </Person>\n     <Person ID=\"Daughter\">\n        <FirstName>Daughter1First</FirstName>\n        <LastName>DaughterTest2</LastName>\n        <BirthCountry>US</BirthCountry>\n        <BirthCity>Denver</BirthCity>\n        <BirthDate>4/27/1969</BirthDate>\n     </Person>\n   </Parents>\n</Family>";

string strTest = "//Person[@ID='Son']/BirthDate";

var doc = new XmlDocument();

doc.LoadXml(xmlstring);

foreach (var node in doc.SelectNodes(strTest))
{
	Console.WriteLine(node);
}

Open in new window


Output:

<BirthDate>12/15/1980</BirthDate>

Open in new window

0
 
käµfm³d 👽Commented:
As wdosanjos demonstrates, when attributes are involved, you have to use the @ syntax to reference them. See http://www.w3schools.com/xpath/xpath_syntax.asp for more information.
0
 
davismAuthor Commented:
Gotchya on that...

However, the string being supplied or used may or may have the attribute. "It someone were to pass a XML string value to me and that string value included a node attribute which is typically enclosed in quotes." (from the OP).

That this would do is restrict that string to having an attribute.

Meaning I could have the:

/Family/Parents/Person ID="Son"/BirthDate

or like a:

/Family/Parents//MotherLastName

In that situation what I was thinkiing was spliting checking to see if the string contains an "=" then using regex's to begin taking the attribute apart. Meaning a regex to identify the atrribute which is to the left of the "=" and to the right of the " " and then get the value within the quotes to the right of the "=" and before the "/". Assign those 2 to variables.

That would help identify and prepare the attribute if there is one.

Outside of that...what was mentioned will definitely get it assuming there is an attribute.

I'm on the fence about whether that is another question, though because it's related. If you all think so,
0
 
käµfm³d 👽Commented:
I think you are taking an incorrect, albeit misguided, approach. Perhaps if you tell us what the ultimate goal is, then we can give you a more elegant approach to what it is you want to accomplish.
0
 
davismAuthor Commented:
The user is going to provide a string of an XPATH. That string may or may not include an attribute. For example:

/Family/Parents/Person ID="Son"/BirthDate

or like a:

/Family/Parents//MotherLastName

Based off that string submitted I would need to get the value of that XMLNode .InnerText from an XMLDocument (that document being like the sample I mentioned above.)

I was doing a SelectSingleNode but the context of the XPath is a little different if there is as attribute (i.e. [@{AttributeName}='{AttributeValue}']/{nodes...}). So I cannot do a SelectSingleNode purely based off the string value supplied unless there is no attribute in the string.
0
 
käµfm³d 👽Commented:
Hmmm...  I can't say I'm a fan of the following approach (or how you're receiving the data), but if you want to keep the input as you've posted above, then you could do a replacement on any attributes. You haven't indicated whether or not multiple attributes are possible, so I am going to assume only one per node is permitted. The following will not work if you have multiple attributes per node in the received XPath.

(I would prefer the user type a proper XPath, but I get the feeling you're developing this for less-technical users, which justifies the change in XPath.)
string xpath = System.Text.RegularExpressions.Regex.Replace(user_string, " ([^=]+)=\"([^\"]*)\"", "[@$1='$2']");

Open in new window

0
 
davismAuthor Commented:
Hmmm...yeah...good point. I guess I could've just replaced the values in the xpath using that regex that you did. I did another one and piecing out the aspects like the attribute name and value. I really didn't need to do that.

You were correct in the only 1 attribute.

When you say you prefer the user type to have a proper xpath, what are you referring to? Because they are supplying the XPath. <confused on what you mentioned>
0
 
käµfm³d 👽Commented:
When you say you prefer the user type to have a proper xpath, what are you referring to?
You said you would receive the XPath as:

/Family/Parents/Person ID="Son"/BirthDate

Open in new window


I understood attribute selection to be:

/Family/Parents/Person[@ID="Son"]/BirthDate

Open in new window


The former is not valid XPath syntax as far as I know. I would expect users to enter the latter.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now