本文介绍了从代码传递名称并从xsd文件中获取该元素的数据类型,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中获得了一个值。该值在xsd文件中显示为 -

xs:element type =xs:bytename =sfFacilityID





我需要在xsd文件中检查这个名字(即sfFacilityID)并获得相同的数据类型c#



我是什么尝试过:



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(doc.Descendants中的var元素(xs +element))

{

datatypexs = element.Attribute(输入)。值;

}

}

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;
}
}

推荐答案

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);
		}
	}
}





上面的代码输出:



电影 - 身份证:1,姓名:蝙蝠侠

电影 - 身份证:2,姓名:蝙蝠侠归来

电影 - 身份证:3,名称:蝙蝠侠黑暗骑士



对于xsd查询和获取数据类型检查此解决方案:

[]


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.:

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;
                            }
                        }


这篇关于从代码传递名称并从xsd文件中获取该元素的数据类型,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:08