|
well you have to add this :
using System.Xml.Xpath;
so that it can identify the XPath.
|
|
|
|
|
Excellent. That does the trick. Thank you.
|
|
|
|
|
|
In order to use SelectNodes to select nodes and attributes that have a namespace prefix, you need an XmlNamespaceManager that knows about the namespace and prefix. Unfortunately, I haven't found a way to access a pre-populated XmlNamespaceManager or to automagically populate one from an XmlDocument.
According to the documentation, XmlReaders use XmlNamespaceManagers, so after I load a document with an XmlReader I should be able to copy the XmlReader's XmlNamespaceManager -- but it shows null (and requires Reflection). I should be able to pass an XmlNamespaceManager to an XmlReader and the XmlReader should use it -- but it still shows null.
I've gone through a bunch of documentation and searching articles here and elsewhere hasn't yielded a silver bullet. They all show code calling AddNamespace to add specific namespaces -- I need it to be dynamic.
So I'm trying to roll my own. At this point, I'm loading the document with an XmlReader and then trying to query any attributes in the xmlns namespace, I should then be able to populate an XmlNamespaceManager with AddNamespace.
The code, as I have it now (I've tried several variations of the XPath) is:
System.Xml.XmlNamespaceManager result =
new System.Xml.XmlNamespaceManager ( Document.NameTable ) ;
System.Xml.XmlNodeList nodes = Document.DocumentElement.SelectNodes ( "//@xmlns:*" , result ) ;
This should yield all the attributes in the xmlns namespace (and the document contains one), but the list comes up empty.
0) Can anyone provide me with an XPath that will do what I want?
1) Does anyone know how to get a pre-populated XmlNamespaceManager from an XmlReader or an XmlDocument?
|
|
|
|
|
I used this:
if (namespace_found)
{
nsmgr = gcnew XmlNamespaceManager (doc->NameTable);
nsmgr->AddNamespace(L"ie", DocNamespace);
}
namespace_found is set to true when a namespace and schema are present on the XML file.
I couldnt add a schema to the XML file at run time.
GER
Ger
|
|
|
|
|
In order to at least have something that works, I now have:
public static System.Xml.XmlNamespaceManager
GetNamespaceManager
(
System.Xml.XmlDocument Document
)
{
System.Xml.XmlNamespaceManager result =
new System.Xml.XmlNamespaceManager ( Document.NameTable ) ;
System.Xml.XmlNodeList nodes = Document.DocumentElement.SelectNodes ( "//*" , result ) ;
foreach
(
System.Xml.XmlNode nod
in
nodes
)
{
foreach ( System.Xml.XmlAttribute att in nod.Attributes )
{
if ( att.Prefix == "xmlns" )
{
result.AddNamespace ( att.LocalName , att.Value ) ;
}
}
}
return ( result ) ;
}
Which gets the job done, and I suppose that's what the framework would be doing anyway so it shouldn't be too much slower. But, if the XmlReader builds one anyway, I sure wish I could access it and not have to build my own.
|
|
|
|
|
There's a Scott Hanselman post here[^] that talks about using an XPathNavigator to get namespaces once it has a currentNode for scope. So this kind of thing works:
XmlNamespaceManager nsmgr = new XmlNamespaceManager( Document.NameTable );
XPathNavigator nav = Document.CreateNavigator();
nav.MoveToFollowing( XPathNodeType.Element );
foreach (var ns in nav.GetNamespacesInScope( XmlNamespaceScope.ExcludeXml ))
{
nsmgr.AddNamespace( ns.Key, ns.Value );
}
As you say, the framework may be doing the same work in each case.
|
|
|
|
|
Hmmm... interesting, thanks, I'll have to take a deeper look. So far I've just looked to see whether or not I can access a NamespaceManager within that, but I didn't see one.
|
|
|
|
|
hello guys,
I am reading xml file and showing in grid in c# but now i want to read specific record(node) of that xml file like where clouse in sqlserver
Example
<Root>
<Data id="1" Name="aaa" Phone="852963" Gender="Male" />
<Data id="2" Name="bbb" Phone="123456789" Gender="Female" />
<Data id="3" Name="ccc" Phone="987654" Gender="Male" />
<Data id="3" Name="ddd" Phone="7676767" Gender="Male" />
</Root>
i need to read the first(TOP)id record and who's Gender="male".
how to do this
Regards
Shafiq
|
|
|
|
|
Try Bipin Joshi's book "Beginning XML with C# 2008" by Apress. I think thats there.
~GER
Ger
|
|
|
|
|
|
Here is the Answer!
XmlDocument xmlDoc =new XmlDocument;
xmlDoc.Load("C:\MyXml");
string myXPath="/Root/Data[@Gender='Male']";
XmlNodeList NodeList=xmlDoc.SelectNodes(myXPath);
XmlNode xmlNod=NodeList(0);
Int myId=xmlNod.Attributes("id").Value;
Cheers! 
|
|
|
|
|
It would be much better if you use XQuery or LINQ which has the same querying pattern like that of SQL. But i am still in the learning phase, so once i get a good hold in it, i shall write my answer.
|
|
|
|
|
XPath is easy to use, uses a hierarchical syntax and is available in many languages.
LINQ is far more flexible, but is .NET-centric.
|
|
|
|
|
hi i am new to xml but i wrote the xml file using xml writer . but the header information is not oming properly. i need the output like this.
<Class xmlns:tns="http://www....." xmlns:xsi="http//...." xsi:text="http://www......">
|
|
|
|
|
Hi All,
I am creating xsd files by using Datasets in my application. I have three datasets, when I create them using dt.WriteXMLSchema, all three datasets are cerating xsd with same class name inside. When I include them in my application its giving me error that same functions are defined.
Is there any way to resolve this problem so that all three xsd should come with their own table names or different names. Here the class names should come differently than "NewDatset".
Any links or any suggestion would be great helpfull. Thanks in advance.
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|
|
Hi All,
I got it done it was simple, just name the dataset with proper name that you want and create the xsd, it will create for you.
But if anybody is having idea of creating the relationships on the xsd as such in the database. If anybody has this idea please help me out by giving links or any suggestions.
It would be great and helpfull for me.
Please help me out. Thanks in advance. In this mean time I will google the things.
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|
|
Is there any way I can get the information I'm using from an xml source to be updated while the program is running?
|
|
|
|
|
Hi,
if your source is an xml file, you could use the FileSystemWatcher[^]-class.
Register for the event of modification and reread the file if the event triggers.
Regards
Sebastian
|
|
|
|
|
[edit]nvm, got it.
modified on Thursday, August 26, 2010 4:27 AM
|
|
|
|
|
Im stuck and need help..
I using the following code to attempt to format a decimal number into £1000.99 for example
<pre>
<fo:table-cell>
<fo:block>
<xsl:value-of select='format-number(".//AmountPaid","#.00")' >
</fo:block>
</fo:table-cell>
</pre>
sample of the XML file is below
<ClaimsHistory>
<LossDate>26/12/2006</LossDate>
<OR>0.0000</OR>
<TypeOfLoss>HA</TypeOfLoss>
<LossDescription>Fire</LossDescription>
<Status>C</Status>
<AmountPaid>41414.4900</AmountPaid>
</ClaimsHistory>
as you can see Im trying to convert the 41414.49 into £41414.90 but all i get is NaN and im stuck can someone please point me in the right direction?
Thanks
Simon
As barmey as a sack of badgers
|
|
|
|
|
In
format-number(".//AmountPaid","#.00")
remove the quotes around .//AmountPaid - with quotes, it won't get interpreted as an XPath expression,rather as a string
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
can anyone tell me how to append xsl output to a file which already has some lines of data.
the quieter u become more u hear
|
|
|
|
|
Get your XSL processor to output to a string and append the string to the existing file
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
XML Schema-
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML referencing XML Schema-
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
I want to know if in xml file I use xsi:noNamespaceSchemaLocation attribute then what elements or attributes should remove from xml schema.
Also please explain
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified"
in xml schema.
Thanks in adv.
|
|
|
|