Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting a value in the code. That value is present in the xsd File as -
xs:element type="xs:byte" name="sfFacilityID"


I need check this name (i.e. sfFacilityID) in the xsd file and get the datatype of the same in c#

What I have tried:

if (templist.Count() <= 0)
{
Comparisionlist cmplist = new Comparisionlist();
cmplist.tagName = words[i];
list.Add(cmplist);
temp = cmplist.tagName;
var xs = XNamespace.Get("~/Content/FacilityInquiryService.xsd1.xsd");
var doc = XDocument.Parse(xml);
foreach (var element in doc.Descendants(xs + "element"))
{
datatypexs = element.Attribute("type").Value;
}
}
Posted
Updated 17-Aug-16 2:31am

Hi nayankumar_msat@hotmail.com

Please see my example below for reading xml (see it working here : XML read dotNetFiddle[^]

I hope this helps in getting you started with reading xml.

C#
using System;
using System.Xml;
					
public class Program
{
	private const string TestXml = 
		"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
		"<MovieData>" +
			"<Movies>" +
				"<Movie>" +
					"<Id>1</Id>" +
					"<Name>Batman</Name>" +
				"</Movie>" +
				"<Movie>" +
					"<Id>2</Id>" +
					"<Name>Batman Returns</Name>" +
				"</Movie>" +
				"<Movie>" +
					"<Id>3</Id>" +
					"<Name>Batman Dark Knight</Name>" +
				"</Movie>" +
			"</Movies>" +
		"</MovieData>";
		
	
	public static void Main()
	{
		XmlDocument xmlDoc = new XmlDocument();
		xmlDoc.LoadXml(TestXml);
		
		string xpath = "MovieData/Movies/Movie";
		var nodes = xmlDoc.SelectNodes(xpath);
		
		foreach(XmlNode childrenNode in nodes)
		{
			Console.WriteLine(
				"Movie - Id: " + childrenNode.SelectSingleNode(".//Id").InnerText + 
				", Name: " + childrenNode.SelectSingleNode(".//Name").InnerText);
		}
	}
}


The code above outputs:

Movie - Id: 1, Name: Batman
Movie - Id: 2, Name: Batman Returns
Movie - Id: 3, Name: Batman Dark Knight

For xsd querying ang getting data types check this solution:
xml - How to extract Attributes along with their names and datatypes from an XSD file using c# - Stack Overflow[^]
 
Share this answer
 
v2
Comments
Nayan_Kumar 16-Aug-16 7:57am    
Thanks njammy for the help.
I'll brief what I am looking for. There is an element in an Xsd file :
xs:element type="xs:float" name="loanProductID"
What I need is in the code I am getting the value - 'loanProductID'.
I need to pass this 'loanProductID' to the xsd file, and fetch its type , that is - float. I want to fetch the datatypes of all the values that are present in the xsd file. Please help me out
njammy 16-Aug-16 8:10am    
Check link in ans.
Nayan_Kumar 16-Aug-16 8:40am    
njammy
I checked out that code earlier in the day in stackoverflow but didnt workout for me. Hence I am stuck. Please help me out as in how can I check for a value in the xsd file and fetch its datatype. I have traversed almost all the places but couldnt find a solution. Your help will be much appreciated. Thanks.
njammy 16-Aug-16 8:44am    
I can't see any other solution other than the stack overflow one. Sorry.
Nayan_Kumar 16-Aug-16 8:51am    
Thanks for your honest answer though !
if my xsd file is like :

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="retrieveFacilityResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="retrieveFaciltyResponse">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="messageHeader">
                <xs:complexType>
                  <xs:sequence>
                 <xs:element name="facility" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:byte" name="sfFacilityID"/>
                    <xs:element type="xs:int" name="sfLoanID"/>
                    <xs:element type="xs:string" name="loanReviewStatus"/>



Got the job done. Below is the code i Used.:

C#
XmlDocument doc = new XmlDocument();
                        doc.Load("Content\FacilityInquiryService.xsd1.xsd");
                        //doc.Load("C:\\WA_Services\\Wholesale Portfolio\\SunTrust\\Wholesale\\Commercial\\Admin\\Admin.Site\\Content\\FacilityInquiryService.xsd1.xsd");
                        XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
                        mgr.AddNamespace("xx", "http://www.w3.org/2001/XMLSchema");
                        foreach (XmlElement el in doc.SelectNodes("//xx:element[@name='retrieveFacilityResponse'][1]/xx:complexType/xx:sequence/xx:element//xx:complexType/xx:sequence/xx:element", mgr))
                        {
                            temp = el.GetAttribute("type");
                            if (el.GetAttribute("name") == words[i])
                            {
                                //temp1.Remove(3);
                                temp1 = (el.GetAttribute("type")).Substring(3, (el.GetAttribute("type")).Length - 3); // (el.GetAttribute("name"));
                                cmplist.Datatype = temp1;
                                break;
                            }      
                        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900