This is probably very simple for you guys, but I need to be able to count the attribute names within an xml document when validating xml against a schema using c#. Specifically duplicate attribute names (not valid).
If my xsd has these in it:
<xsd:schema>
<xsd:complexType name="scheduleEvent">
<xsd:all>
<xsd:element name="Basic" type="MyBasic"/>
</xsd:all>
</xsd:complexType>
<xsd:complexType name="MyBasic">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="Descriptor" type="Descriptor" maxOccurs="1"/>
<xsd:element name="Descriptor1" type="Descriptor1" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="Descriptor">
<xsd:attribute name="Test" type="typ:Test"/>
</xsd:complexType>
<xsd:complexType name="Descriptor1">
<xsd:attribute name="Test1" type="typ:Test1"/>
</xsd:complexType>
And my xml to validate against looks like (not valid, I know just a quick mock up example for reference:
Declarations etc...
<ScheduleEvent>
<MyBasic>
<Descriptor Test="02"/>
<Descriptor1 Test1="02" Test1="02"/>
</MyBasic>
</ScheduleEvent>
How can I count the number of "Test1" attributes? C# xmlreader (without using exceptions, long story).
First of all, that isn't a valid xml. An attribute is unique per element.
If what you ment is that you can have multiple "Test1" attributes throughout the different elements, then you can do the following using LINQ to XML:
var xml = XDocument.Load(#"PathToXml");
var testCount = xml.Descendants().Attributes("Test1").Count();
Try this :
XDocument doc=XDocument.Load(XMLFile or XMLFilePath);
var Sample=doc.Descendants("Test1").Count();
Related
I need to call a Web Service, created by someone else, from a .Net C# application.
The web service requires me to send a list of items ("articles"), without enclosing them in a parent element. This is a simplified illustration of the "desired" look of the call:
<body>
<info>something</info>
<moreinfo>something else</moreinfo>
<article>
<articleno>123</articleno>
</article>
<article>
<articleno>456</articleno>
</article>
</body>
On my end, the articles is a generic List<Article>, and my call to the web service tends to contain something looking more like this:
<body>
<info>something</info>
<moreinfo>something else</moreinfo>
<articles><!-- parent element -->
<article>
<articleno>123</articleno>
</article>
<article>
<articleno>456</articleno>
</article>
</articles><!-- parent element end -->
</body>
The WSDL file from the web service is corrupted, so I've hade to manually create a working subset of the functions in it that I need. Using that "hand coded" WSDL I add a Service Reference using Visual Studio 2013.
I have been trying to solve the problem by modifying the WSDL, and by modifying the code that is created by Visual Studio when adding the service reference, but so far I haven't succeeded.
I've bee trying various attributes for the list parameter, such as XmlElement, XmlIgnore, MessageHeaderArrayAttribute and others, but to be honest I don't know how they work or which ones could actually be useful in this scenario.
Can the WSDL be altered to make Visual Studio's auto generated code create the correct output when calling the web service? (Preferred solution)
Or is there a way to force soap serialization to produce the list of articles without an enclosing element? (Acceptable solution)
Here is a simplified version of the relevant (as far as I can tell) portions of the WSDL
<xsd:element name="PriceRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="info" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="moreinfo" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="articles" type="tns:ArrayOfArticlestructObj" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
...
<xsd:complexType name="ArticlestructObj">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="articleno" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1" name="otherinfo..." type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ArticlestructObj" type="tns:ArticlestructObj" />
<xsd:complexType name="ArrayOfArticlestructObj">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="ArticlestructObj" nillable="true" type="tns:Articlestruct" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="ArrayOfArticlestruct" type="tns:ArrayOfArticlestruct" />
I am a noob on WSDL as well as on StackOverflow, so please excuse me if this question is hard to read.
My understanding of XML isn't exactly stunning, but my understanding of content in annotation/appinfo is that you're allowed any well-formed XML, and this is lax validated. My understanding of lax validation is that elements and attributes will be validated if the relevant schema information can be obtained.
My situation is that I have an XML schema with some content in this section that requires validating. I have the relevant schemas to validate the content against.
I've taken a totally simple XML schema and added an appinfo element to it, as shown below. The content of the appinfo is another element declaration, purely for simplicity of not referencing another schema. The schema I'm trying to validate obviously references something different.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.demo.org"
xmlns="http://www.demo.org"
elementFormDefault="qualified">
<xsd:annotation>
<xsd:appinfo>
<xsd:element name="Demo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="A" type="xsd:iteger"/>
<xsd:element name="B" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="Demo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="A" type="xsd:integer"/>
<xsd:element name="B" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Note in the appinfo section, I've deliberately mis-spelled "integer" on element A. If I do this in the main body of the schema, then XmlSchemaSet.Compile() will tell me this schema isn't valid.
However, if I use the schema as it's written above, it tells me there is no problem. In the more complex file I started with, I supplied the external schema to XmlSchemaSet via XmlSchemaSet.Add()
I've also tried loading the XML schema from http://www.w3.org/2001/XMLSchema.xsd and loading the schema to be validated into an XmlDocument and running XmlDocument.Validate() but no joy.
I feel like I'm totally missing something totally fundamental after spending hours on this. Any pointers appreciated!
This is not my understanding of lax: if you have a declaration
<any
namespace=". . . "
processContents="lax">
</any>
the content of the corresponding element in the XML being validated will be validated against the schemas corresponding to the namespace(s) specified by namespace (if any) - not against any schema it might be using. The declaration for appInfo is
<xs:element name="appinfo" id="appinfo">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:any processContents="lax"/>
</xs:sequence>
<xs:attribute name="source" type="xs:anyURI"/>
<xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:complexType>
</xs:element>
without any namespace specification, that corresponds to namespace="##any", that means that there is no validation against any schema.
I have the following in a WSDL I am consuming;
<xsd:complexType name="SomeClassType">
<xsd:sequence>
<xsd:element type="xsd:string" name="errorMessage" minOccurs="1" nillable="true" maxOccurs="1"> </xsd:element>
<xsd:element type="tp:ArrayOfArrayOfString" name="values" minOccurs="1" nillable="true" maxOccurs="1"> </xsd:element>
<xsd:element type="xsd:boolean" name="isEmpty" minOccurs="1" maxOccurs="1"> </xsd:element>
</xsd:sequence>
</xsd:complexType>
where
<xsd:complexType name="ArrayOfArrayOfString">
<xsd:complexContent>
<xsd:restriction base="soapenc:Array">
<xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[,]"></xsd:attribute>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
However using wsdl.exe from MS (Runtime Version: 1.1.4322.573) generates
public class SomeClassType {
///
public string errorMessage;
///
public string[] values;
///
public bool isEmpty;
}
I expected string[,] values not string[] values
Is there a fix or a work around to this problem? (other than manually changing the generated code)
I had to set the type="tp:ArrayOfArrayOfString" to type="tp:ArrayOfString" and the maxOccurs="unbounded"
I believe you could try the WCF proxy generator (I believe WCF uses some other util, not wsdl.exe) - maybe that would be useful, but if that fails - I think that manually editing the generated code is your only option.
Try svcutil.exe. it is advisable to try a newer version of .net.
Does anyone have an idea about how keep track of a paths in xsd in deep-first traverse
For example: if I have this schema,
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:bks="urn:books">
<xsd:element name="books" type="bks:BooksForm"/>
<xsd:complexType name="BooksForm">
<xsd:sequence>
<xsd:element name="book" type="bks:BookForm" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BookForm">
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float" />
<xsd:element name="pub_date" type="xsd:date" />
<xsd:element name="review" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
how can I get like this output in c#
path1 : books.book.author
path2 : books.book.title
.. so on
for any schema structure
does anyone have an idea about this or any good starting points
thanks in advance
thanks for reply
I tried to code your advice but I am still getting stuck
while (r.Read())
{
switch (r.Name)
{
case "xsd:element":
myStringBuilder.Append(r.GetAttribute("name"));
break;
case "xsd:complexType":
checkComplex(r);
wholepath += r.GetAttribute("name");
//this will only concatenate complex elements only
Console.WriteLine("checkComplexcaller{1}", wholepath);
break;
}
}
for recursive part.. I did
public static void checkComplex(/*what I should send here*/)
{
if (r.GetAttribute("name") == "xsd:complexType")
{
//What I sould to do for this recursive
}
else if (r.GetAttribute("name") == "xsd:element")
{
myStringBuilder.Append(r.GetAttribute("name"));
}
}
How could setup the path correctly
If I am getting this right: (pseudocode)
(make a xml document of the xsd)
foreach node of type xsd:element:
track path to the node as input
apend it with values of 'name' and 'type' attributes
if its a complex type: apply this recursively on all elements
Good day.
As i know. There is a root element in XML file.
But from the XSD file structure, it is not easy to get the root element value.
Is there any method to do it?
(I wouldn't like to take use of hard code to find XSD root element value in my project.
i want to find the root element of "RootValueHere"
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="RootValueHere">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="DocumentInfo" minOccurs="1" maxOccurs="1" />
<xsd:element ref="Prerequisite" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!-- Element of DocumentInfo -->
<xsd:element name="DocumentInfo">
<xsd:complexType>
<xsd:attribute name="Name" type="xsd:string" />
<xsd:attribute name="Description" type="xsd:string" />
<xsd:attribute name="Creator" type="xsd:string" />
<xsd:attribute name="CreateTime" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<!-- Element of Prerequisite -->
<xsd:element name="Prerequisite">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Type" type="Prerequisite.Type.type" minOccurs="1" maxOccurs="1" />
<xsd:element name="Miscellaneous" type="Prerequisite.Misc.type" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
thank you.
Whilst a single document can only contain one root element, as XSD can actually define multiple valid root elements.
If you only genuinely want a single type to be valid as a root element, then it should be the only type referenced as an <element>.
In your schema above, for example, the DocumentInfo and Prerequisite nodes are valid root elements too. To restrict your schema to have just a single, valid root node, replace your DocumentInfo and Prerequisite elements with simple complexType definitions:
<xsd:complexType name="DocumentInfoType">
...
</xsd:complexType>
<xsd:complexType name="Prerequisite">
....
</xsd:complexType>
UPDATE: To access the name of an element, you just need to look at the Name property on the XmlElement:
XmlDocument doc = new XmlDocument();
doc.Load("D:\\schema.xsd"); // Load the document from the root of an ASP.Net website
XmlElement schemaElement = doc.DocumentElement; // The root element of the schema document is the <schema> element
string elementName = schemaElement.LocalName; // This will print "schema"
foreach (XmlNode ele in schemaElement.ChildNodes)
{
if (ele.LocalName == "element")
{
// This is a valid root node
// Note that there will be *more than one* of these if you have multiple elements declare at the base level
}
}
I believe
XmlDocument myDocument = new XmlDocument("my.xml");
myDocument.DocumentElement(); //gets root document node