For example,
input string is like
C. Qiao andR.Melhem , "Reducing Communication ",1995 DATE>.
I want this output:
C AUTHOR
. AUTHOR
Qiao AUTHOR
and
R AUTHOR
. AUTHOR
Melhem AUTHOR
,
"
Reducing TITLE
Communication TITLE
"
,
1995 DATE
.
1. XML String is valid (that is, there will be no invalid characters between tags)
Like this:
string xml = @""; C. Qiao
R.Melhem
Reducing Communication TITLE>
1995
2. The split will be completed by space”
string xml = @""; C. Qiao
R.Melhem
Reducing Communication
1995
XElement doc = XElement.Parse(xml);
foreach (XElement element in doc.Elements())< br /> {
var values = element.Value.Split('');
foreach (string value in values)
{
Console.WriteLine(element .Name + "" + value);
}
}
It will be printed out
AUTHOR C.
AUTHOR Qiao
AUTHOR R.Melhem
TITLE Reducing
TITLE Communication
TITLE
DATE 1995
Edit:
Now , Split based on “.” and spaces, the best idea is to use regular expressions. Like this:
var values = Regex.Split(element.Value, @"(\.| )");
foreach (string value in values.Where(x=>!String.IsNullOrWhiteSpace( x)))
{
Console.WriteLine(element.Name + "" + value);
}
If you want, you can add more separators. The following example will give you the following:
AUTHOR C
AUTHOR .
AUTHOR Qiao
AUTHOR R
AUTHOR .< br />AUTHOR Melhem
TITLE Reducing
TITLE Communication
DATE 1995
EDIT2:
This is an example used with the original string, it is very likely Not the best way, because it does not have the correct token order, but it should be very close:
string xml = @"";
C. Qiao
and
R.Melhem ,
""Reducing Communication ""
,< DATE>1995.
//Parse xml to XDocument
XDocument doc = XDocument.Parse(xml);
// Get first element (we only have one)
XElement element = doc.Descendants().FirstOrDefault();
//Create a copy of an element for use by child elements.
XElement copyElement = new XElement(element);
//Remove all child nodes from root leaving only text
element.Elements( ).Remove();
//Splitting based on the tokens specified
var values = Regex.Split(element.Value, @"(\.| |\,|\"" )");
foreach (string value in values.Where(x => !String.IsNullOrWhiteSpace(x)))
{
Console.WriteLine(value);
}
//Getting children nodes and splitting the same way
foreach (XElement elem in copyElement.Elements())
{
var val = Regex.Split(elem.Value, @ "(\.| |\,|\"")");
foreach (string value in val.Where(x => !String.IsNullOrWhiteSpace(x)))
{
Console.WriteLine(value + "" + e lem.Name);
}
}
//You can try to play with DescendantsAndSelf
//to see if you can do it in single action and with order preserved.< br /> //foreach (XElement elem in element.DescendantsAndSelf())
//{
// //....
//}
This The following will be printed out:
and
,
"
"
,
.
C AUTHOR
. AUTHOR
Qiao AUTHOR
R AUTHOR
. AUTHOR
Melhem AUTHOR
Reducing TITLE
Communication TITLE
1995 DATE
I want to split the XML Like string into c# or sql tags.
For example,
input string is like
C. Qiao andR.Melhem , "Reducing Communication ",1995 .
I want this output:
C AUTHOR
. AUTHOR
Qiao AUTHOR
and
R AUTHOR
. AUTHOR
Melhem AUTHOR
,
"
Reducing TITLE
Communication TITLE
"
,
1995 DATE
.
Considering the following factors ,This is the first attempt to solve this problem:
1. XML String is valid (that is, there will be no invalid characters between tags)
Like this:
< p>
string xml = @""; C. Qiao
R.Melhem
Reducing Communication
1995
2. The split will be completed by space
string xml = @""; C. Qiao
R.Melhem
Reducing Communication < br />1995
XElement doc = XElement.Parse(xml);
foreach (XElement element in doc.Elements())
{
var values = element.Value.Split('');
foreach (str ing value in values)
{
Console.WriteLine(element.Name + "" + value);
}
}
will be printed out p>
AUTHOR C.
AUTHOR Qiao
AUTHOR R.Melhem
TITLE Reducing
TITLE Communication
TITLE
DATE 1995
Edit:
Now, to split based on “.” and spaces, the best idea is to use regular expressions. Like this:
var values = Regex.Split(element.Value, @"(\.| )");
foreach (string value in values.Where(x=>!String.IsNullOrWhiteSpace( x)))
{
Console.WriteLine(element.Name + "" + value);
}
If you want, you can add more separators. The following example will give you the following:
AUTHOR C
AUTHOR .
AUTHOR Qiao
AUTHOR R
AUTHOR .< br />AUTHOR Melhem
TITLE Reducing
TITLE Communication
DATE 1995
EDIT2:
This is an example used with the original string, it is very likely Not the best way, because it does not have the correct token order, but it should be very close:
string xml = @"";
C. Qiao
and
R.Melhem ,
""Reducing Communication ""
,1995 .
//Parse xml to XDocument
XDocument doc = XDocument.Parse(xml);
// Get first element (we only have one)
XElement element = doc.Descendants().FirstOrDefault();
//Create a copy of an element for use by child elements.
XElement copyElement = new XElement(element );
//Remove all child nodes from root leaving only text
element.Elements().Remove();
//Splitting based on the tokens specified
var values = Regex.Split(element.Value, @"(\.| |\,|\"")");
foreach (string value in values.Where(x => !String.IsNullOrWhiteSpace( x)))
{
Console.WriteLine(value);
}
//Getting children nodes and splitting the same way
foreach (XElement elem in copyElement.Elements())
{
var val = Regex.Split(elem.Value, @"(\.| |\,|\"")");
foreach (string value in val.Where(x =>! String.IsNullOrWhiteSpace(x)))
{
Console.WriteLine(value + "" + elem.Name);
}
}
//You can try to play with DescendantsAndSelf
//to see if you can do it in single action and with order preserved.
//foreach (XElement elem in element.DescendantsAndSelf())
//{< br /> // //....
//}
This will print out the following:
and
,
"
"
,
.
C AUTHOR
. AUTHOR
Qiao AUTHOR
R AUTHOR
. AUTHOR
Mel hem AUTHOR
Reducing TITLE
Communication TITLE
1995 DATE