I found that if I use the Xelement, new Select with to extract a set of XML value,
It extract some extra text which are not needed.
For example, In the code below, It gives me something like the below which I don't need the { desc = } signs..
{ desc = Students with PMP professional certification may apply for substitution for ISOM 5460. }
Protected Sub getDescrItem(ByVal name As String)
Dim xele As XElement = XElement.Load(MapPath("~\xml\course_desc.xml"))
Dim xitems = (From x In xele.Descendants(name).Descendants("desc").Descendants() _
Select New With { _
.desc = x.Attribute("text").Value
})
For Each x In xitems
MsgBox(x.ToString)
corenameXML.Text = x.ToString
Next
End Sub
<desc>
<descitem text="Students with CISSP, CISM or GCIH professional certification may apply for substitution for ISOM 5280." />
<descitem text="Students with PMP professional certification may apply for substitution for ISOM 5460." />
</desc>
Is there anyway I can get rid of the unwanted signs?
Also, If i use Xdocument, the value of the text can be extracted exactly for what i want. But What is the diffecence by using Xdocument.load and XElement.load? (Since I cannot use FirstOrDefault when I use Xelement.load).
Thank you for your great help in advance!!
Function getName(ByVal name As String) As String
Dim xele As XDocument = XDocument.Load(MapPath("~\xml\course_desc.xml"))
Dim xname As XElement = xele.Descendants(name).Descendants("name").FirstOrDefault
Return xname.Attribute("text")
End Function
string descXML =
"<desc>" +
"<descitem text=\"Students with CISSP, CISM or GCIH professional certification may apply for substitution for ISOM 5280.\"/>" +
"<descitem text=\"Students with PMP professional certification may apply for substitution for ISOM 5460.\"/>" +
"</desc>";
XElement xe = XElement.Parse(descXML); //or you can use XElement.Load if the param is file name.
IEnumerable<XAttribute> xDescColl = from s in xe.Descendants().Attributes("text")
select s;
string myDescriptions = "";
foreach (XAttribute xDesc in xDescColl)
{
myDescriptions += " " +xDesc.Value;
}
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Marked as answer by Bee90124 on Apr 14, 2012 10:02 AM
Bee90124
Member
33 Points
73 Posts
Text Format Extract by LINQ-XML
Apr 12, 2012 03:05 PM|LINK
Hello,
I found that if I use the Xelement, new Select with to extract a set of XML value,
It extract some extra text which are not needed.
For example, In the code below, It gives me something like the below which I don't need the { desc = } signs..
{ desc = Students with PMP professional certification may apply for substitution for ISOM 5460. }Protected Sub getDescrItem(ByVal name As String) Dim xele As XElement = XElement.Load(MapPath("~\xml\course_desc.xml")) Dim xitems = (From x In xele.Descendants(name).Descendants("desc").Descendants() _ Select New With { _ .desc = x.Attribute("text").Value }) For Each x In xitems MsgBox(x.ToString) corenameXML.Text = x.ToString Next End Sub<desc> <descitem text="Students with CISSP, CISM or GCIH professional certification may apply for substitution for ISOM 5280." /> <descitem text="Students with PMP professional certification may apply for substitution for ISOM 5460." /> </desc>Is there anyway I can get rid of the unwanted signs?
Also, If i use Xdocument, the value of the text can be extracted exactly for what i want. But What is the diffecence by using Xdocument.load
and XElement.load? (Since I cannot use FirstOrDefault when I use Xelement.load).
Thank you for your great help in advance!!
Function getName(ByVal name As String) As String Dim xele As XDocument = XDocument.Load(MapPath("~\xml\course_desc.xml")) Dim xname As XElement = xele.Descendants(name).Descendants("name").FirstOrDefault Return xname.Attribute("text") End Functiontehremo
Star
10540 Points
1704 Posts
Re: Text Format Extract by LINQ-XML
Apr 12, 2012 05:17 PM|LINK
Try modifying your query:
Dim xitems = (From x In xele.Descendants(name).Descendants("desc") _
Select New With { _
.desc = x.Attribute("text").Value
})
kavita_khand...
Star
9767 Points
1930 Posts
Re: Text Format Extract by LINQ-XML
Apr 13, 2012 10:37 AM|LINK
string descXML = "<desc>" + "<descitem text=\"Students with CISSP, CISM or GCIH professional certification may apply for substitution for ISOM 5280.\"/>" + "<descitem text=\"Students with PMP professional certification may apply for substitution for ISOM 5460.\"/>" + "</desc>"; XElement xe = XElement.Parse(descXML); //or you can use XElement.Load if the param is file name. IEnumerable<XAttribute> xDescColl = from s in xe.Descendants().Attributes("text") select s; string myDescriptions = ""; foreach (XAttribute xDesc in xDescColl) { myDescriptions += " " +xDesc.Value; }I would love to change the world, but they wont give me the source code.