I think you should use LINQ-TO-XML to deal with the problem by using Descedants to find out the specific element of xsd and then do outputting like following this:
【Sample & Solution】
namespace CSharp
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApplication7
{
public class Program
{
static void Main(string[] args)
{
var result = from e in XDocument.Load("XMLFile1.xml").Descendants(XNamespace.Get("http://www.w3.org/2001/XMLSchema") + "complexType")
where e.Attribute("name").Value.Equals("EXP")
select e.Descendants(XNamespace.Get("http://www.w3.org/2001/XMLSchema") + "element");
//Loop out all the ComplexType
foreach (var item in result)
{
//Loop out all the element type nested in the Complex type
foreach (var sitem in item)
{
Console.WriteLine(sitem.Attribute("name").Value);
}
}
}
}
}
}
do you know the best way to iterate over this
Arraylist (at bold)in my view to output select options?
//Loop out all the ComplexType
foreach (var item in result)
{
//Loop out all the element type nested in the Complex type
foreach (var sitem in item)
{
Console.WriteLine(sitem.Attribute("name").Value);
types.Add(sitem.Attribute("name").Value);
}
}
ViewData["types"] = types;
return View();
view:
<div class="editor-label">
<label for="type">Type</label>
</div>
<div class="editor-field"> //select with options here
QuantumInfor...
Member
199 Points
228 Posts
Help with c# XPath
Apr 28, 2012 03:17 PM|LINK
HIi
how would I select all the names (inbold?) for the <xsd:complexType name="EXP">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation xml:lang="en">
</xsd:documentation>
</xsd:annotation>
<xsd:element name="TEXP" type="EXP"/>
<xsd:complexType name="EXP">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="test" maxOccurs="unbounded"/>
<xsd:element name="test2" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attributeGroup ref="INFO"/>
</xsd:complexType>
<xsd:simpleType name="string-or-empty">
<xsd:union memberTypes="xsd:string empty-string" />
</xsd:simpleType>
etc
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Help with c# XPath
Apr 30, 2012 01:10 AM|LINK
Hello:)
I think you should use LINQ-TO-XML to deal with the problem by using Descedants to find out the specific element of xsd and then do outputting like following this:
【Sample & Solution】
namespace CSharp { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace ConsoleApplication7 { public class Program { static void Main(string[] args) { var result = from e in XDocument.Load("XMLFile1.xml").Descendants(XNamespace.Get("http://www.w3.org/2001/XMLSchema") + "complexType") where e.Attribute("name").Value.Equals("EXP") select e.Descendants(XNamespace.Get("http://www.w3.org/2001/XMLSchema") + "element"); //Loop out all the ComplexType foreach (var item in result) { //Loop out all the element type nested in the Complex type foreach (var sitem in item) { Console.WriteLine(sitem.Attribute("name").Value); } } } } } }QuantumInfor...
Member
199 Points
228 Posts
Re: Help with c# XPath
Apr 30, 2012 07:54 AM|LINK
thanks mate, could you explain what this is for:
XNamespace.Get("http://www.w3.org/2001/XMLSchema") + "element"
cheers :)
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Help with c# XPath
Apr 30, 2012 08:04 AM|LINK
XNamespace.Get("http://www.w3.org/2001/XMLSchema"):This is a namespace defined here:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
so when you are referring a node name with a defined namespace,you should use XNameSpace.Get to refer the correct namespace……
QuantumInfor...
Member
199 Points
228 Posts
Re: Help with c# XPath
Apr 30, 2012 01:04 PM|LINK
sweet :) lastly :
do you know the best way to iterate over this Arraylist (at bold)in my view to output select options?
//Loop out all the ComplexType
foreach (var item in result)
{
//Loop out all the element type nested in the Complex type
foreach (var sitem in item)
{
Console.WriteLine(sitem.Attribute("name").Value);
types.Add(sitem.Attribute("name").Value);
}
}
ViewData["types"] = types;
return View();
view:
<div class="editor-label">
<label for="type">Type</label>
</div>
<div class="editor-field">
//select with options here
</div>
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Help with c# XPath
May 01, 2012 05:33 AM|LINK
Suppose your "types" is a collection of string,you've saved into ViewData——So you can do this:
<div class="editor-label">
<label for="type">Type</label>
</div>
<div class="editor-field">
<%
foreach(string s in ((IEnumerable<string>)ViewData["types"])
{
//Deal with s
}
%>
</div>