How to retrieve the xml element attribute name in asp.net - asp.net

I need to retrieve the xml attribute values in asp.net. Here i cant retrieve data from xml. Can anybody help me. Thanks in advance.

this might help you ....
Here is how my XML looks like:
<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
<Category>
<MainCategory ID="1">VC++</MainCategory>
<Description>A list of VC</Description>
<Active>Yes</Active>
</Category>
</CategoryList>
add the value of the element MainCategory to the drop down list. I used the SelectNodes function to get the values and stored it while iterating through a loop. This looked like this:
XmlNodeList nodes = xmlDoc.SelectNodes("/CategoryList/Category");
for(int i=0;i<nodes.Count;i++)
{
ddlMainCategory.Items.Add(new ListItem(
nodes.Item(i).ChildNodes[0].InnerText,
nodes.Item(i).ChildNodes[0].Attributes["ID"].Value
));
}

Related

Datapower- Reading the output context of Convert Query Params in next action

I am using Convert Query params to XML to convert from JSON to JSONX. The output of this action is stored in Jsonx_Out. I have a transform action with Jsonx_Out as input. Could anyone please help me out how I can read this context. I tried with dp:variable(' var://context/Json_Out'). This does not fetch the value.
Thanks.
Well, if it is the input context for your transform action, you can access it as you would any other XML input:
<xsl:template match="/">
<xsl:apply-templates select="/json:object"/>
</xsl:template>
(Don't forget to define the json namespace.)
You should be able to access a context with dp:variable as you described. I do notice you left out the 'x' in Jsonx_Out -- that may be your issue.
Are you sure you are getting JSONX?
Query Params outputs a XML so to get a param, eg. param1 from: http://server.com/uri?param1=HelloWorld
You'd use:
<xsl:variable name="param1">
<xsl:value-of select="/request/args/arg[#name='param1']"/>
</xsl:variable>
The XSL var "param1" would then contain "HelloWorld"

How to add Xml File Into Project(after deployment) Using User Interface?

I have one DemoQuestion.Xml,after deploying project i want to upload DemoQuestion.xml file with different Question Using USer Interface(such as Admin add New Question To test)
DemoQuestion.xml
<?xml version="1.0" encoding="UTF-8"?>
<quiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="quiz.xsd">
<mchoice>
<question>Sum of 20 and 30?</question>
<answer>20</answer>
<answer correct="yes">50</answer>
<answer>10</answer>
<answer>11</answer>
</mchoice>
<mchoice>
</quiz>
i want to add <answer Correct="yes">This attribute in xml how to do it?
Here's a minimalistic code snippet...
string file = Server.MapPath("~/file.xml");
XmlDocument doc = new XmlDocument();
doc.Load(file);
var answers = doc.SelectNodes("//answer");
if (answers != null && answers.Count > 0)
{
XmlAttribute attr = doc.CreateAttribute("correct");
attr.Value = "yes";
answers[0].Attributes.Append(attr);
}
doc.Save(file);
All it does is load the document from a file, retrieves all answer elements and adds the "correct" attribute with the value of "yes" to the first answer found.
Hope it helps illustrate the solution
Leo

How can i extract XML and use it as a datasource?

I am using asp.net VB and I have an XML file containing a set of data, I would like to use it in something like a datalist and where usually you would use a database i would like to use the XML file to produce the information.
Does anyone know how to do this, i have read about transform files but surely i will format the information in the control?
The file has multiple records so in some cases i would need to perform queries on the information through the datasource.
I would maybe look into XML serialization and de-serialization. Using de-serialization you could read your XML into a List(T) object containing a list of your own class objects and use that as a data source for your application.
Heres a link that you may find useful:
http://msdn.microsoft.com/en-us/library/ms731073.aspx
Hope this helps.
Dim ds As New DataSet()
ds.ReadXml(MapPath("data.xml"))
First you have to parse the XML and store that into custom C# object or you can directly pass the XML to your stored procedure and do the codding there for saving it into DB.
Passing the xml to stored procedure and manipulating it there is bit difficult so what I suggest is to parse it in C# and then get a custom object. Once you get it you can do whatever you want to.
Below is the sample code that parse a XML file and generate a custom C# object from it.
public CatSubCatList GenerateCategoryListFromProductFeedXML()
{
string path = System.Web.HttpContext.Current.Server.MapPath(_xmlFilePath);
XDocument xDoc = XDocument.Load(path);
XElement xElement = XElement.Parse(xDoc.ToString());
List<Category> lstCategory = xElement.Elements("Product").Select(d => new Category
{
Code = Convert.ToString(d.Element("CategoryCode").Value),
CategoryPath = d.Element("CategoryPath").Value,
Name = GetCateOrSubCategory(d.Element("CategoryPath").Value, 0), // Category
SubCategoryName = GetCateOrSubCategory(d.Element("CategoryPath").Value, 1) // Sub Category
}).GroupBy(x => new { x.Code, x.SubCategoryName }).Select(x => x.First()).ToList();
CatSubCatList catSubCatList = GetFinalCategoryListFromXML(lstCategory);
return catSubCatList;
}

Very confused about how to parse xml with namespace prefixes

So, I need to be able to parse xml files that could include namespace prefixes. I've tried doing this with a sample file and it gives me back null when trying to get a nodelist, even when I specify a node that has no attribute prefixes.
I've been trying to research this and it keeps coming back to the fact that without the namespace prefix defined, it won't work, so I've added code that I thought would do this, but it's still giving the same results. Here's some code I've added:
protected void Page_Load(object sender, EventArgs e)
{
xml.Load(Server.MapPath("~/SomeLesson/imsmanifest.xml"));
populateBaseNodes();
}
private void populateBaseNodes()
{
treeViewMenu.Nodes.Clear(); // Clear any existing items
TreeNode treenode = new TreeNode();
treenode.Text = "organizations";
XmlNodeList baseNodeList;
string xmlns = xml.DocumentElement.Attributes["xmlns"].Value;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("adlcp", "http://www.adlnet.org/xsd/adlcp_v1p3");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("imscp", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("imsss", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
baseNodeList = xml.SelectNodes("/manifest/organizations/organization/item", nsmgr);
TextBox1.Text = baseNodeList.Count.ToString();
foreach (XmlNode xmlnode in baseNodeList)
{
TreeNode treeNodeCatalog = new TreeNode();
treeNodeCatalog.Text = xmlnode.Attributes["identifier"].Value;
treeNodeCatalog.SelectAction = TreeNodeSelectAction.Expand;
treeViewMenu.Nodes.Add(treeNodeCatalog);
}
treeViewMenu.CollapseAll();
}
(marc_s) Here's the XML in question that needs to be parsed:
<manifest identifier="Navigating_in_QuickBooks_-_Introduction_MANIFEST" version="1.3"
xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:imscp="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_v1p3"
xmlns:imsss="http://www.imsglobal.org/xsd/imsss"
xsi:schemaLocation=" http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd
http://www.imsglobal.org/xsd/imsss imsss_v1p0.xsd
http://www.adlnet.org/xsd/adlcp_v1p3 adlcp_v1p3.xsd
http://www.adlnet.org/xsd/adlseq_v1p3 adlseq_v1p3.xsd
http://www.adlnet.org/xsd/adlnav_v1p3 adlnav_v1p3.xsd">
<metadata>
<!-- not relevant here ... -->
</metadata>
<organizations default="TOC1">
<organization identifier="TOC1">
<title>Navigating in QuickBooks - Introductory Lesson</title>
<item identifier="I_SCO0" identifierref="SCO0">
<title>Navigating in QuickBooks - Introductory Lesson</title>
</item>
</organization>
</organizations>
<resources>
<!-- not relevant here ... -->
</resources>
</manifest>
You're not showing us what your XML looks like - but two comments:
you don't need to add the xsi prefix, and I'm not sure what the schemaLocation prefix is supposed to do ....
when you've defined the schema prefixes, you also need to use those prefixes in your XPath, of course!
Again, not knowing what your XML structure looks like, I cannot really tell what you need - but something along the lines of:
baseNodeList = xml.SelectNodes("/adlcp:manifest/adlcp:organizations/adlcp:organization/imscp:item", nsmgr);
or whatever other XML namespace prefixes your source XML requires.
Update: seeing your XML makes it clearer: see the root node - it has a default XML namespace (the one with xmlns="...." and no explicit prefix):
<manifest identifier="Navigating_in_QuickBooks_-_Introduction_MANIFEST" version="1.3"
xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" <=== DEFAULT namespace!!!
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:imscp="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_v1p3"
xmlns:imsss="http://www.imsglobal.org/xsd/imsss"
................>
That means: ALL your subsequent nodes that don't have a specific XML prefix will be in that default namespace.
Unfortunately, .NET XML parsing has problem with defining a default namespace without prefix - so my best solution is to create a namespace with a prefix for the default namespace, and then use it:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
// add default namespace, with a prefix for .NET
nsmgr.AddNamespace("ns", "http://www.imsglobal.org/xsd/imscp_v1p1");
baseNodeList =
xml.SelectNodes("/ns:manifest/ns:organizations/ns:organization/ns:item", nsmgr);
Do you get any results now??
In the XML you posted the default namespace controlling all of the elements in your sample file is:
xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"
This namespace does not define a prefix so you must add this namespace to your namespace manager using a blank prefix. I think you should be able to use this code to define the default namespace (using String.Empty to specify a blank prefix):
nsmgr.AddNamespace(String.Empty, "http://www.imsglobal.org/xsd/imscp_v1p1");

How to get two xml files in a single array collection in flex?

I have two xml files, defect. xml and employee.xml. But the files havea common field but with different names in each file. I want both the files to be merged in to a single array collection.
The structure of my defect.xml file is:
<defectList>
<defect>
<revId>123</revId>
<revType>IQA</revType>
<status>Review Pending</status>
<assignedTo>Angeline</assignedTo>
<loggedBy>chandran</loggedBy>
<closedDate>13-10-2009</closedDate>
</defect>
<defect>
<revId>124</revId>
<revType>IQA</revType>
<status>Review Pending</status>
<assignedTo>Aarthi</assignedTo>
<loggedBy>chandran</loggedBy>
<closedDate>15-10-2009</closedDate>
</defect>
<defectList>
And my employee.xml
<Employees>
<employee>
<employeeId>256148</employeeId>
<employeeName>Angeline</employeeName>
</employee>
<employee>
<employeeId>256158</employeeId>
<employeeName>Aarthi</employeeName>
</employee>
<Employees>
I get both the xml files in two array collections:
<mx:Model id="employeeXML" source="assets/employee.xml"/>
<mx:ArrayCollection id="employeeList" source="{employeeXML.employee}"/>
<mx:Model id="defectXML" source="assets/defect.xml"/>
<mx:ArrayCollection id="defectList" source="{defectXML.defect}"/>
when "assignedTo" matches "employeeName", I want the "employeeId" to be added to the defectList array collection. How can I do this?
How to iteerate through an array collection?
And how to check if the assignedTo field in defectList equals employeeName field in employeeList? Some one guide me..
EDIT
ok, Now I'm able to compare the two fields,assignedTo of defectList and employeeName of employeeList.Thanks to Simon:
var defect:Object;
var employee:Object;
for each (defect in defectList)
{
for each (employee in employeeList)
{
if(defect.assignedTo == employee.employeeName)
{
// defectList.addItem(employee.employeeId);
// I tried this,but it is wrong .
}
}
}
But how to add the employeeId field to that particular array element,so that I could use defectList as the dataprovider for the datagrid n display employee Id also? Can someone help me with this.
SOLUTION
I have found out the solution. Here is the code:
public function init():void{
var defect:Object;
var employee:Object;
for each (defect in defectList)
{
for each (employee in employeeList)
{
if(defect.assignedTo == employee.employeeName)
{
var id:Object;
id=employee.employeeId;
defect["employeeId"]=id;
}
}
}
}
Now, if I give datafield as "employeeId" in the datagrid with defectList as dataprovider, I get the employee id of the corresponding Employee Name.
I can answer part of this...
Iterating through an ArrayCollection is easy...
var defect:Object;
var employee:Object;
for each (defect in defectList)
{
for each (employee in employeeList)
{
// do your check here.
}
}
You need to be careful with iterators like ForEach if you are hanging the contents of the arrays as you iterate. You may want to consider loading up a third array as you go.
I'm not sure exactly how you get at the element values from the XML objects created in your code snippet, but there is a lot of help on the Adobe Livedocs
You might be able to use the fact that an object can have its fields reference in a late-binded way...
var elementContents:String = employee["employeeName"];

Resources