Thursday, February 26, 2009

Some Xml Stuff's

Serializing A Entity Class To String:
Dim commandResult As Object
commandResult = commandObject.Execute(inputValue) ‘Command Result is list of Some Entity class
Dim xmlSerializer As XmlSerializer xmlSerializer = New XmlSerializer(commandResult.GetType())
Dim memStream As MemoryStream = New MemoryStream()
xmlSerializer.Serialize(memStream, commandResult)
Dim xdoc As System.Xml.XmlDocument = New System.Xml.XmlDocument() 'Set the pointer to begining of the stream memStream.Position = 0
xdoc.Load(memStream)
Dim strResult As String = xdoc.OuterXml
Return (strResult)

Reading from XML String Using Linq to XML:
xmlContent is of String format.Using Linq u can get each Xml Element or Attribute in order to get the data in Entity Class format.
Dim xmlDocument As XDocument = XDocument.Parse(xmlContent)
Dim Claims = New ClaimUI WITH _ { _ .ID = ClaimUI.Element("ClaimID"), _ .Reference = ClaimUI.Element("ClaimReference") _ } End With _ In Dim FoundNames As IEnumerable(Of String) = From Name In AllNames Where Name.StartsWith("G") 'When this statement has been executed, FoundNames will now contain a collection of names that starts with the letter "G." It this case, it returns "Gabriel."
You can now add the names to a ListBox control:
For Each PeopleName In FoundNames

ListBox1.Items.Add(PeopleName)
Next
'To Find a author Where author.Field(Of String)("State") = "CA" _
Dim C As ClaimUI = New ClaimUI()
Dim Claims = From Claim In xmlDocument.Descendants("ClaimEntity") _
Select New ClaimUI With { .ID = Claim.Element("ClaimID"), _ .Reference = Claim.Element("ClaimReference")} .LastName = ClaimUI.Field(Of String)("au_lname".) }
ProductList.ItemsSource = Claims
ClaimList.ItemsSource = Claims

Handling Or Converting Entire Record Set in Form of XML In Stored Procedure Level:
Declare @XML VARCHAR(MAX)
SET @XML ='Root Tag'
SET @XML = @XML + (SELECT * FROM tblCaseReg for XML Path('Records'))
SET @XML = @XML + 'Root Tag'
SELECT @XML
Creating or Forming Xml using XML Document:
Dim Doc As New XmlDocument()

Dim newAtt As XmlAttribute
Dim TempNode As XmlElementTrypropertiesList = New PropertiesEntityList
powerStationDataReader = CType(ReportingDAL.Sample(), SqlDataReader) 'which wil return me datareader
Dim dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", _Nothing, Nothing)

Doc.AppendChild(dec)
Dim DocRoot As XmlElement = Doc.CreateElement("Root")

Doc.AppendChild(DocRoot)
list = New List(Of Object)Dim x As Integer
Dim colname1 As String = powerStationDataReader.GetName(x).ToString()
Dim columncount1 As Integer = powerStationDataReader.FieldCount
For x = 0 To columncount1 - 1
Dim pv1 As XmlNode = Doc.CreateElement("Column")
newAtt = Doc.CreateAttribute("colname")
DataValue = colname1
newAtt.Value = DataValue.ToString()
pv1.Attributes.Append(newAtt)
DocRoot.AppendChild(pv1)
NextWhile (powerStationDataReader.Read())
Dim SqlReader As SqlClient.SqlDataReader = powerStationDataReader
Dim columncount As Integer = powerStationDataReader.FieldCount
For x = 0 To columncount - 1
Dim colname As String = SqlReader.GetName(x).ToString()

Dim pv As XmlNode = Doc.CreateElement("NodeItem") ' Creating Each Node for xml
newAtt = Doc.CreateAttribute(colname)
DataValue = powerStationDataReader(x) 'Data For the Attribute
newAtt.Value = DataValue.ToString()
pv.Attributes.Append(newAtt)
DocRoot.AppendChild(pv) 'Adding Node to the Doc Root
Next
End While
stringXML = Doc.OuterXml 'Finally u can have it in string format.

Extracting XML Data:
Dim xmlReaderForSettings As XmlReader = xmlReaderForSettings.Create(New StringReader(XmlStringData))
While (xmlReaderForSettings.Read()) 'Reading Each Xml Node
If (xmlReaderForSettings.HasAttributes) Then 'Now for Attributes if it has Gor then Find for the Elements Key NameIf (xmlReaderForSettings.IsStartElement("Settings")) Then
While (xmlReaderForSettings.MoveToNextAttribute()) 'Similarly move to all Attributes
If (xmlReaderForSettings.Name = "ID") Then
_Id= xmlReaderForSettings.Value
ElseIf (xmlReaderForSettings.Name = "Name") Then

_Name=xmlReaderForSettings.Value
End While
End If
End If
End While

No comments: