I want to get the contents of a sql table using xml:
projectID - projectName - customerID -customerName - city
i want to list all columns in xml tags and nest the customer seperately inside a project element
how can i then use this in .net correctly?
You can transform the table row into XML using by using xmlelement:
(select xmlelement (name Project,
xmlattributes(p.projectID as id),
xmlelement(p.name as Name),
xmlelement(name Customer,
xmlattributes(p.customerID as id),
xmlforest(p.customerName as Name, p.city as City)
))
)
from
TableName p
You need to replace 'TableName' with whatever your table is called. This basically returns data as XML and nests a new customer element inside a parent project tag. This is the XML outputted:
<Project id="1">
<Project Name>Manhatten Project</Project>
<Customer id="200">
<Name>Jim Doe</Name>
<City>New York</City>
</Customer>
</Project>
You can then parse the XML in .net with the Read() method. If you've not used XML with .net before, read this article for a general introduction.
Related
I have created a web service using ASP.NET and am now creating a client application to consume it.
The service allows the user to create playlist and add songs etc The lists are stored in a XMl file with the following structure:
<?xml version="1.0" encoding="utf-8"?>
<Playlists>
<Lists>
<List ID="3" user="dylan">
<Track name="Sorry">
<Artist>Justin Bieber</Artist>
<Album>Purpose</Album>
<Genre>Pop</Genre>
</Track>
<Track name="Thriller">
<Artist>Michael Jackson</Artist>
<Album>Thriller</Album>
<Genre>Pop</Genre>
</Track>
<Track name="Hello">
<Artist>Adele</Artist>
<Album>25</Album>
<Genre>Pop</Genre>
</Track>
</List>
</Lists>
</Playlists>
I am looking to extract the various track names, artist, album and genre and display them in a Windows form List Box.
Currently, my code allows me to select a list. However, rather than display the name, artist, album then genre, the list box displays the following (please ignore the data. in this version, I accidentally added 2 instances of Michael Jackson 'Thriller', I'm not that much of a fan! ;) Also, Thriller in this photo is the Album:
I cannot seem to be able to select the "name" attribute.
Any help would be much appreciated. I would like to keep the XML structure the same if possible.
I have posted the code I am using in the client app below.
Public Class Form1
Dim serviceReturn As String
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim service As New PlaylistReference.ServiceSoapClient
Try
serviceReturn = service.GetPlaylist(TextBox1.Text)
MessageBox.Show(serviceReturn)
Catch ex As Exception
MessageBox.Show("A stock quote for this ticker was not available.")
End Try
Dim xmlPlaylist As New Xml.XmlDocument
xmlPlaylist.LoadXml(serviceReturn)
Dim xmlSong As Xml.XmlNode
For Each xmlSong In xmlPlaylist.DocumentElement
Dim songName As String = xmlPlaylist.LastChild.InnerText
ListBox1.Items.Add(songName)
Next
End Sub
End Class
So all that happens is I enter the name of the playlist into TextBox1 and then the press of the button gets the output above. Please ignore the MessageBox function, this is just for testing purposes.
I believe your question can be narrowed down to: I cannot seem to be able to select the "name" attribute.
Attributes are in the Attributes property, i.e. xmlSong.Attributes("name").Value
I have to read element from Inbound Header...
I am assigning inbound header using WCF.InboundHeaders to a string....
now my problem is my inbounde header is looking like this
InboundHeaders
<headers><s:userid xmlns:s="http://www.w3.org/2003/05/soap-envelope">testuser</s:userid>
<s:applicationid xmlns:s="http://www.w3.org/2003/05/soap-envelope">assistworkerweb</s:applicationid>
<a:Action s:mustUnderstand="1" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">http://Request</a:Action><a:To s:mustUnderstand="1" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
Now i need to extract user id from it ..how to extract user id from it..
You haven't mentioned where or how your string is stored (that is populated with your WCF.InboundHeaders), however I would use a simple fragment of XPath to extract the UserId. If you were extracting this using a C# helper, you could do something along the lines of (note, this is untested, however its pretty much there):
XmlDocument doc = new XmlDocument();
doc.Load([WCF.InboundHeaders Xml Fragment]);
// Create an XmlNamespaceManager to add 'soap-envelope' namespace
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("s", "http://www.w3.org/2003/05/soap-envelope");
// Select the UserId
XmlNode userId = doc.SelectSingleNode("/headers/s:userid", nsmgr);
Console.WriteLine(userId.InnerXml);
You may also want to serialize the Xml fragment into a .Net object and retrieve the UserId in that manner.
I am using SDL tridion 2011 SP1.
I want to get the list of keywords under given category using XSLT Mediator.
Have any one come across this situation, If yes please share your views.
But When I actually looked at the Category Item XML it does not have any info related to its keywords.
You will need to create a C# TBB to insert the category keywords into the package, and then access this as a parameter in your XSLT.
You can use a piece of c# like the following:
class GetCategoryKeywords : TemplateBase
{
public override void Transform(Engine engine, Package package)
{
Initialize(engine, package);
String webDavPathCategory = package.GetValue("CategotryWebDavPath");
Category cat = (Category)engine.GetObject(webDavPathCategory);
XmlDocument keywordsXml = new XmlDocument();
keywordsXml.LoadXml(cat.GetListKeywords().OuterXml);
Item output = package.CreateXmlDocumentItem(ContentType.Xml, keywordsXml);
package.PushItem("CategoryKeywords", output);
}
}
This will place an XML document in the package called CategoryKeywords containing the keywords. Then when you invoke the XSLT mediator, set the "Include Package Paramters" value to true, and add a parameter to the top of your XSLT as follows:
<xsl:param name="CategoryKeywords"/>
You can then loop through the new parameter as a variable performing any XPath queries on it that you desire. The following samples may help:
<xsl:variable name="URI" select="$CategoryKeywords//tcm:ListUsedItems/tcm:Item[#Title=$VALUE]/#ID" />
<xsl:for-each select="$CategoryKeywords//tcm:ListItems/tcm:Item">
Do something
<xsl:for-each>
I implemented a ribbon tool bar button for Tridion 2011 SP1, which opens an aspx page and populates a drop down list based on a look-up component. The look-up component comprises of different embedded schemas. To filter out the values based on embedded schema name I need to get the Embedded schema field values of component creation page on button click in button JavaScript.
Because in my component creation page consists of multivalued Embedded schema field has the info, which helps look up value filtering process. I am unaware of the command need to be used for the requirement. I know about a command to get the complete component XML, that is: $display.getView().getItemFields().
To get the present RTF field content I am going for the command: target.editor.getHTML(). To get the complete set of Embedded schema field values only,
which command I need to use?
My sample component source:
<root>
<a>sample a</a>
<b>sample b</b>
<c>
<ca>ca 1</ca>
<cb>cb 1</cb>
<cc>cc 1</cc>
</c>
<c>
<ca>ca 2</ca>
<cb>cb 2</cb>
<cc>cc 2</cc>
</c>
<c>
<ca>ca 1</ca>
<cb>cb 1</cb>
<cc>cc 1</cc>
</c>
</root>
I don't think there are public API for that. But you could use component data xml and then parse it by yourself:
var item = $display.getItem();
var xml = item.getContent(); // OR $display.getView().getItemFields();
var xmlDoc = $xml.getNewXmlDocument(xml);
var schema = item.getSchema();
if(schema.isLoaded())
{
var xpath = "/custom:{0}/custom:embeddedFieldName".format(schema.getRootElementName());
var fields = $xml.selectNodes(xmlDoc, xpath, { custom: schema.getNamespaceUri() });
// loop fields and get values ...
}
I am working on Tom.Net APIs in SDL Tridion 2011 SP1.
I am trying to retrieve the "source" part of the XhtmlField.
My source look like this.
<Content>
<text>
<p xmlns="http://www.w3.org/1999/xhtml">hello all<strong>
<a id="ID1" href="#" name="ZZZ">Name</a>
</strong></p>
</text>
</Content>
I want to get the source of this "text" field and process the tags with name a.
I tried following:
ItemFields content = new ItemFields(sourcecomp.Content, sourcecomp.Schema);
XhtmlField textValuesss = (XhtmlField)content["text"];
XmlElement textxmlelement = textValuesss.Definition.ExtensionXml;
Response.Write("<BR>" + "count:" + textxmlelement.ChildNodes.Count);
for (int i = 0; i < textxmlelement.ChildNodes.Count; i++)
{
Response.Write("<BR>" + "nodes" + textxmlelement.ChildNodes[i].Name);
}
//get all the nodes with the name a
XmlNodeList nodeswithnameA = textxmlelement.GetElementsByTagName("a");
foreach (XmlNode eachNode in nodeswithnameA)
{
//get the value at the attribute "id" of node "a"
string value = eachNode.Attributes["id"].Value;
Response.Write("<BR>" + "idValue" + value);
}
I am not getting any output. More over I am getting the count as Zero.
Output I got:
count:0
Though I have some child tags in the field, I am not getting why 0 is coming as Count.
Can any suggest the modification needed.
Thank you.
ItemField.Definition gives access to the Schema Definition of the field, and not the field content, so you should not use the ExtensionXml property to access content (thats why its empty). This property is used for storing extension data in the schema definition.
To work with fields containing XML/XHTML content I would just access the Content property of the component as this is already an XmlElement. You need to be careful about the namespace of the content, so use an XmlNamespaceManager when querying this XmlElement. For example the following will give you a reference to the field named 'text':
XmlNameTable nameTable = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nameTable);
nsManager.AddNamespace("custom", sourceComp.Content.NamespaceURI);
XmlElement fieldValue = (XmlElement)sourceComp.Content.SelectSingleNode(
"/custom:Content/custom:text", nsManager);
textValuesss.Definition.ExtensionXml
This is the wrong property (Definition leads to the Schema field definition and ExtensionXml is for custom XML data written by an extension).
You want to use textValuesss.Value instead and load it as XML. After that, you should probably use SelectSingleNode with a specific XPath query that includes the XHTML namespace. Or use Linq to XML to find the elements.