It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to find the line in a multi-line string, containing XML, that a given Xpath points to.
In other words, I have a string containing XML. I have a given Xpath, that I want to use to find the exact line line number of the string.
What's the best way to do this?
You might consider using a an XmlTextReader instead. That has LineNumber and LinePosition properties.
The XmlTextReader does have some limited "read until you reach such-and-such a node" properties, but they aren't as sophisticated as those of XPath. Depending on how complex the XPath is, this might or might not work.
I did find a link on the subject here.
You can load the XML from the string into an actual XML class.
XDocument x = XDocument.Parse(yourStringContainingXML);
string contentOfNode = x.Descendants(XName.Get("NameOfTheNode")).First().Value;
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am new to learning Regex and I am struggling with this basic issue. I want to make sure a string is in a format like: 2000/2001 or 2010/2011.
I tried something like: ^[2000-2900]./.[2000-2900]$ but I know this is wrong!
This would be the very basic:
^\d{4}\/\d{4}$
From the beginning of the string, check if it has 4 digits followed by a "/" (escaped with "\") and another 4 digits to the end of the string.
If you searching for where the entire string must match then:
^\d{4}/\d{4}$
If you are searching for a sub string of a larger string then:
\d{4}/\d{4}
And if you using in C# then remember to wrap it up in a verbatim string like so:
#"^\d{4}/\d{4}$"
#"\d{4}/\d{4}"
I noticed that others are escaping the forward slash but I don't think is necessary but doesn't do any harm if you do.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need to find the mentioned lambda statement and remove it from my code. My project is so big, and I noticed in the find and replace box of Visual Studio, there is an argument that can use regular expressions to find and replace codes. Is there a regular expression that can find this statement completely (contains line break and white space also)?
() =>
{
CallMethod()
},
I'm afraid that VS IDE is using the regular expressions in single line mode (which is actually strange considering that it offers \n in the suggestion menu). I think you will be a lot better creating a new project, which will load the file, read all text from it, and replace whatever regex you specify, and then save the file back.
Basically the regex you need is this:
(\(\ *\)\ *=\>[\r\n\s\{\}]*CallMethod\ *\(\ *\)[\r\n\s\{\}]*,)
In C# code, you can do it like this:
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(#"(\(\ *\)\ *=\>[\r\n\s\{\}]*CallMethod\ *\(\ *\)[\r\n\s\{\}]*,)", System.Text.RegularExpressions.RegexOptions.Multiline);
regex.Replace(document, string.Empty);
Hope this will be of help to you.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to get the data between the two <br/> tags.
The data in xml file is like this <br/>lots of data<br/>.
Now I want to get the data between the two <br/> tags and fetch specific data in it.
Can anyone suggest me how to search the tags and fetch the specific data between them?
Well you could use some LINQ to XML here to do this.
Example:
var xmlStr = #"<root>
data1<br/>
data2<br/>
data3<br/>
data4<br/>
data5<br/>
</root>";
var doc = XDocument.Parse(xmlStr);
var query =
from br in doc.Descendants("br")
let textNode = br.NextNode as XText
where textNode != null
let nextBr = textNode.NextNode as XElement
where nextBr != null && nextBr.Name == "br"
select textNode.Value;
LINQ to XML approach is fairly more approachable than this as Jeff Mercado's answer. As using RegEx is not recommended for parsing XML data. But if your requirement is for one time only then it might help.
(?is)(?<=^|<br/>).*?(?=<br/>)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I need to write a simple parser that will convert the tokens to parser tree.
I've already wrote LexicalAnalyzer that returns the tokens. Now, I want
to write rules for "if and while" statements(for the beginning), so I could pass this rules to parser and it will create a tree.
So i need to write the parser in the way, so I could write new rules.
Can you advise me how I can implement it in C#? Can you give me some example?
In a recursive descent parser it's easy to implement these statements if you have the normal block and expression parsers. In pseudo-code, they are basically:
void ParseIf()
{
Match("if");
Match("(");
ParseExpression();
Match(")");
ParseBlock();
}
and
void ParseWhile()
{
Parse("while");
Parse("(");
ParseExpression();
Parse(")");
ParseBlock();
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In the case that there were two text files:
FileA.txt
test
1234
testing
FileB.txt
test
5667
pond
and all occurrences in FileA.txt would be removed from FileB.txt, being output into FileC.txt
So FileC.txt would read:
5667
pond
File.WriteAllLines("FileC.txt",
File.ReadAllLines("FileB.txt").Except(File.ReadAllLines("FileA.txt")));
string fileA, fileB, fileC;
var result = File.ReadAllLines(fileB).Except(File.ReadAllLines(fileA));
File.WriteAllLines(fileC, result);
I'm not sure how your text files are formatted, but you can use StreamReader to load and read through the lines of the text. First, do that to A, add each line to an array, and then filter through the array for each line of B to see if there is a match. If so, remove that line from B before creating C with StreamWriter.
Read about streamreader here. Read about streamwriter here.