Need Help About Using XPathNavigator in C#? - c#

My XML file as below. It mixed schema and normal elements.
<?xml version="1.0" encoding="utf-8"?>
<!-- R1 -->
<ax:root xmlns:ax="http://amecn/software/realtime/ax">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="EquipmentConstants">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" ref="EquipmentConstant" />
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="id">
<xsd:selector xpath=".//EquipmentConstant" />
<xsd:field xpath="#id" />
</xsd:unique>
</xsd:element>
......
......
</xsd:schema>
<EquipmentConstants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EquipmentConstant id="0">
<Name>SerialNumber</Name>
<Group>SYSTEM</Group>
<Data>
<Value min="0" max="10000000" scale_factor="0" unit="U_NO_UNITS" permission="NolimitedAndNoChangeable" type="xsd_string" enum="" flag="0">0</Value>
</Data>
<Description>Serial Number</Description>
</EquipmentConstant>
.....
.....
</EquipmentConstants>
</ax:root>
My C# code as below. I want to loop the elements start from (by pass all the content of schema)
<EquipmentConstants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
XPathDocument doc = new XPathDocument("test.xml");
XPathNavigator navigator = doc.CreateNavigator();
navigator.MoveToRoot(); // <?xml version="1.0" encoding="utf-8"?>
//navigator.MoveToFirstChild(); // <!-- R1 -->
// 1st, I tried to use MoveToChield(), But I failed to move there.
navigator.MoveToChild("EquipmentConstants");
// Then, I also tried to use SelectSingleNode(). But I failed too.
navigator.SelectSingleNode("ax/EquipmentConstants");
while (navigator.MoveToNext())
{
// do something.
}
Could you please give me some suggestion. Thank you.

XPathNavigator navigator = doc.CreateNavigator();
if (navigator == null)
{
return;
}
foreach (XPathNavigator nav in
navigator.Select("/" + "EquipmentConstants" + "/" + "EquipmentConstant"))
{
}

My solution as below.
XPathDocument doc = new XPathDocument("test.xml");
XPathNavigator navigator = doc.CreateNavigator();
navigator.MoveToRoot(); // <?xml version="1.0" encoding="utf-8"?>
navigator.MoveToFirstChild(); // <!-- R1 -->
navigator.MoveToNext(); // <ax:root xmlns:ax="http://amecn/software/realtime/ax">
navigator.MoveToChild("EquipmentConstants", ""); // <EquipmentConstants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
navigator.MoveToFirstChild(); // <EquipmentConstant id="0">
do
{
// Loop body;
} while (navigator.MoveToNext());

Related

insert xsi:schemaLocation and xlmns and prefix in XML

I am trying to insert xsi and xsmln data into XML, I have used various ways but I could not get it, I also need to insert prefixes. Next I show my code where I get the XML and I also show you the structure I get, and at the end I show you what I really want to get.
This is the code where I get the XML:
string txtXML = XmlfrommyfunctionSQL(); // here retrieve from sqlserver
XDocument doc;
using (StringReader s = new StringReader(txtXML))
{
doc = XDocument.Load(s);
}
doc.Declaration = new XDeclaration("1.0", "UTF-8", null);
string targetxml = doc.ToString();
targetxml = doc.Declaration.ToString() + Environment.NewLine + doc.ToString();
This is the XML I get in string targetxml:
<?xml version="1.0" encoding="UTF-8"?>
<Invoice>
<UBLxtensions>
<UBLExtension>
<AccountingSupplierParty>
<AdditionalAccountID>1</AdditionalAccountID>
<Party>
<PartyName>
<Name>GRUPO ERB</Name>
</PartyName>
<PhysicalLocation>
<Address>
<ID>11001</ID>
<Country>
<IdentificationCode>CO</IdentificationCode>
</Country>
</Address>
</PhysicalLocation>
</Party>
</AccountingSupplierParty>
</UBLExtension>
</UBLExtensions>
</Invoice>
But I need to insert xsi:schemaLocation and xmlns, and insert prefix in Elemnts, how do I do that ?
I expect to get this:
<?xml version="1.0" encoding="UTF-8"?>
<Invoice Invoice xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2
http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#"
xmlns:xades="http://uri.etsi.org/01903/v1.3.2#"
xmlns:sts="http://www.dianees.com/contra/acturaeca/v1/Structures"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<cac:AccountingSupplierParty>
<cbc:AdditionalAccountID>1</cbc:AdditionalAccountID>
<cac:Party>
<cac:PartyName>
<cbc:Name>GRUPO ERB</cbc:Name>
</cac:PartyName>
<cac:PhysicalLocation>
<cac:Address>
<cbc:ID>11001</cbc:ID>
<cac:Country>
<cbc:IdentificationCode>CO</cbc:IdentificationCode>
</cac:Country>
</cac:Address>
</cac:PhysicalLocation>
</cac:Party>
</cac:AccountingSupplierParty>
</ext:UBLExtension>
</ext:UBLExtensions>
</Invoice>
please show me how to make one and I will do the rest
This will give you an idea how to solve your problem. I created a small example. it is far from perfect but is shows in basic which steps to take.
first the no namespace xsd is defined as:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Parent">
<xs:complexType>
<xs:sequence>
<xs:element name="Child1" type="xs:string"/>
<xs:element name="Child2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
then because of the different namespaces in your message i've created the same message with different namespaces. Each namespace is a different file.
Root.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/Root" xmlns:p="http://tempuri.org/Parent" targetNamespace="http://tempuri.org/Root" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://tempuri.org/Parent" schemaLocation="Parent.xsd"/>
<xs:element name="Root" type="p:ParentType"/>
</xs:schema>
parent.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/Parent" xmlns:ch="http://tempuri.org/Child" targetNamespace="http://tempuri.org/Parent" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://tempuri.org/Child" schemaLocation="Child.xsd"/>
<xs:complexType name="ParentType">
<xs:sequence>
<xs:element name="Parent" type="ch:ChildType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Child.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns="http://tempuri.org/Child1" targetNamespace="http://tempuri.org/Child">
<xs:complexType name="ChildType">
<xs:sequence>
<xs:element name="Child1" type="xs:string"/>
<xs:element name="Child2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Next step is to create class files using xsd.exe. I use a bat file to execute te commands.
#echo off
set exepad=C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\xsd.exe
set sourcepad=..\..\
set xsdpad=%sourcepad%\console\xsd
set Outpad=%sourcepad%\console\generated
Set NameSpace=Console.generated
"%exepad%" "%xsdpad%\NoNSmessage.xsd" /c /n:%NameSpace% /o:%Outpad%
"%exepad%" "%xsdpad%\Child.xsd" "%xsdpad%\Parent.xsd" "%xsdpad%\Root.xsd" /c /n:%NameSpace% /o:%Outpad%
pause
Then import the generated class files into your project so they can be used.
this is an example how you can map the fields from the source to the target.
static void Main(string[] args)
{
const string msg1 = "<Root><Parent><Child1>First</Child1><Child2>Second</Child2></Parent></Root>";
//deserialize xml string into class object.
XmlSerializer deserializer = new XmlSerializer(typeof(Root));
var reader = new StringReader(msg1);
var noNamespaceRoot = (Root)deserializer.Deserialize(reader);
//map the fields from the nonamespace msg to the msg with a namespace
var namespaceRoot = new ParentType();
namespaceRoot.Parent = new ChildType();
namespaceRoot.Parent.Child1 = noNamespaceRoot.Parent.Child1;
namespaceRoot.Parent.Child2 = noNamespaceRoot.Parent.Child2;
//serialize the class object to a string.
var serializer = new XmlSerializer(typeof(ParentType));
var sww = new StringWriter();
using (XmlWriter writer = XmlWriter.Create(sww))
{
serializer.Serialize(writer, namespaceRoot);
}
System.Console.WriteLine(sww.ToString());
}
the output will be:
<?xml version="1.0" encoding="utf-16"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/Root">
<Parent xmlns="http://tempuri.org/Parent">
<Child1 xmlns="http://tempuri.org/Child">First</Child1>
<Child2 xmlns="http://tempuri.org/Child">Second</Child2>
</Parent>
</Root>

XML namespace missing for parser

I have to parse a XML where the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace is missing, so the xml looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<program>
<scriptList>
<script type="StartScript">
<isUserScript>false</isUserScript>
</script>
</scriptList>
</program>
but should look like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<program xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<scriptList>
<script xsi:type="StartScript">
<isUserScript>false</isUserScript>
</script>
</scriptList>
</program>
The type attribute ensures the correct subclass e.g.
class StartScript : script
{...}
The parser is auto generated from an handwritten xsd via $> xsd.exe a.xsd /classes (.Net).
Here is the xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="qualified">
<!-- Main element -->
<xs:element name="program">
<xs:complexType>
<xs:sequence>
<xs:element name="scriptList">
<xs:complexType>
<xs:sequence>
<xs:element name="script" type="script" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="script" />
<xs:complexType name="StartScript">
<xs:complexContent>
<xs:extension base="script">
<xs:all>
<xs:element name="isUserScript" type="xs:boolean"></xs:element>
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>
A simple solution is to run a string-replace (" type=\"" to " xsi:type=\"") on the input XML but this is pretty ugly.
Is there a better solution?
You can load your XML into an intermediate LINQ to XML XDocument, fix the attribute namespaces on the <script> elements, then deserialize directly to your final class:
// Load to intermediate XDocument
XDocument xDoc;
using (var reader = XmlReader.Create(f))
xDoc = XDocument.Load(reader);
// Fix namespace of "type" attributes
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
foreach (var element in xDoc.Descendants("script"))
{
var attr = element.Attribute("type");
if (attr == null)
continue;
var newAttr = new XAttribute(xsi + attr.Name.LocalName, attr.Value);
attr.Remove();
element.Add(newAttr);
}
// Deserialize directly to final class.
var program = xDoc.Deserialize<program>();
Using the extension method:
public static class XObjectExtensions
{
public static T Deserialize<T>(this XContainer element, XmlSerializer serializer = null)
{
if (element == null)
throw new ArgumentNullException();
using (var reader = element.CreateReader())
return (T)(serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
}
}

Reading in XML from WPF c#

I'm struggling to read in a "GPX" file to a WPF (c#) project. Sample GPX is provided below. I've tried a number of different options with the same result.
Document is loading ok, but I'm unable to break it down to access the Nodes Directly.
Any help would be greatly appreciated.
Thanks.
private void Simple_Click(object sender, RoutedEventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(#"C:\Users\Jonathon\Desktop\GPX_Data.gpx");
XmlNodeList nodes = xml.SelectNodes("trkpt"); // have tried: double '/' to get nodes at any level (XPath syntax)
//XmlNodeList nodes = xml.SelectNodes("/gpx/trk/trkseg/trkpt");
int count = 0;
foreach (XmlNode xn in nodes)
{
count++;
}
}
}
Sample GPX File
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="Endomondo.com"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd
http://www.garmin.com/xmlschemas/GpxExtensions/v3
http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd
http://www.garmin.com/xmlschemas/TrackPointExtension/v1
http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata>
<author>
<name>Jonathon Ralfe</name>
<email id="jonathon" domain="ralfe.net"/>
</author>
<link href="http://www.endomondo.com">
<text>Endomondo</text>
</link>
<time>2015-01-27T18:31:26Z</time>
</metadata>
<trk>
<src>http://www.endomondo.com/</src>
<link href="https://www.endomondo.com/workouts/463986953/2256850">
<text>endomondo</text>
</link>
<type>SKIING_DOWNHILL</type>
<trkseg>
<trkpt lat="45.576892" lon="6.894079">
<time>2015-01-26T09:49:57Z</time>
</trkpt>
<trkpt lat="45.576892" lon="6.894079">
<ele>1595.0</ele>
<time>2015-01-26T09:49:59Z</time>
</trkpt>
<trkpt lat="45.577109" lon="6.893946">
<ele>1581.0</ele>
<time>2015-01-26T09:51:46Z</time>
</trkpt>
<trkpt lat="45.5772" lon="6.894084">
<ele>1575.0</ele>
<time>2015-01-26T09:52:02Z</time>
</trkpt>
<trkpt lat="45.577247" lon="6.894212">
<ele>1577.0</ele>
<time>2015-01-26T09:52:05Z</time>
</trkpt>
<trkpt lat="45.577317" lon="6.89452">
<ele>1589.0</ele>
<time>2015-01-26T09:52:11Z</time>
</trkpt>
That's because your xml contains namespaces, so you have to set namespace when querying data.
Consider this approach:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("x", "http://www.topografix.com/GPX/1/1");
XmlNodeList nodes = xml.SelectNodes("//x:trkpt", nsmgr);
Here we're creating NamespaceManager, setting namespace according to your data xmlns="http://www.topografix.com/GPX/1/1" attribute and using this namespace in XPath.
By XPath selection:
foreach(XElement aElement in xml.XPathSelectElements("/trk/trkseg").Elements())
{
foreach(XNode aXNode in aElement.Nodes())
{
//Access subnodes of trkpt
}
}

How to change a value of an attribute?

in the last days i tried to change a value of a single attribute of this application File
The Xml-File:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd"
manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns="urn:schemas-microsoft-com:asm.v2"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"
xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"
xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1"
xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
<assemblyIdentity name="lolz" version="1.1.1.1" publicKeyToken="12345" language="neutral" processorArchitecture="x86" xmlns="urn:schemas-microsoft-com:asm.v1" />
<description asmv2:publisher="Example" asmv2:product="Productexample2" asmv2:supportUrl="Example" xmlns="urn:schemas-microsoft-com:asm.v1" />
<deployment install="true" mapFileExtensions="true" minimumRequiredVersion="1.1.1.1" trustURLParameters="true">
<subscription>
<update>
<beforeApplicationStartup />
</update>
</subscription>
<deploymentProvider codebase="http://Test" />
</deployment>
</asmv1:assembly>
Here i try to change the value of
<description asmv2:product = "Productexample">
into
<description asmv2:product = "Productexample2">
,and the value of
<deploymentProvider codebase="http://Test" />
into
<deploymentProvider codebase="http://Test2" />
Currently i tried :
private void changeAttribute (string xmlPath)
{
string newValue = "Productexample2";
XmlDocument xmlDoc= new XmlDocument();
xmlDoc.Load(xmlPath);
XmlNode node = xmlDoc.SelectSingleNode("asmv1:assembly/description/asmv2:product");
node.Attributes[0].Value = newValue;
xmlDoc.Save(xmlPath);
}
But it throws the Exception 'An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll' so i think asmv1:assembly/description/asmv2:product is wrong ...
any suggestions of code?
as always you can correct me in any way :)
You have to use a namespace manager to make the prefixes work and you need the # character to indicate it is an attribute you are referencing.
So this should work:
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");
manager.AddNamespace("asmv2", "urn:schemas-microsoft-com:asm.v2");
XmlNode node = xmlDoc.SelectSingleNode("/asmv1:assembly/asmv1:description/#asmv2:product", manager);
Also, your first line in the XML root is incomplete. It should be:
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd"
manifestVersion="1.0"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns="urn:schemas-microsoft-com:asm.v2"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"
xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"
xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1"
xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2"
>

Xml Xsd Validation Fails (xs:anyType)

I have this XML file
<bookstore>
<test>
<test2/>
</test>
</bookstore>
and this XSD schema
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="bookstore" type="bookstoreType"/>
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="test" type="xsd:anyType" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
I intend to validate xml file from C# code.
There is a method that validate XML file:
// validate xml
private void ValidateXml()
{
_isValid = true;
// Get namespace from xml file
var defaultNamespace = XDocument.Load(XmlFileName).Root.GetDefaultNamespace().NamespaceName;
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.Schemas.Add(defaultNamespace, XsdFileName);
settings.ValidationEventHandler += OnValidationEventHandler;
// Create the XmlReader object.
using(XmlReader reader = XmlReader.Create(XmlFileName, settings))
{
// Parse the file.
while (reader.Read()) ;
}
}
private void OnValidationEventHandler(object s, ValidationEventArgs e)
{
if (_isValid) _isValid = false;
if (e.Severity == XmlSeverityType.Warning)
MessageBox.Show("Warning: " + e.Message);
else
MessageBox.Show("Validation Error: " + e.Message);
}
I know, this XML file is valid. But my code reterns this Error:
Validation Error: Could not find schema information for the element 'test2'
Where is my mistake?
Thanks!!!
UPDATE: I assume your code matches the error you listed (I've tried your code on .NET 3.5SP1, and I wasn't able to reproduce your behaviour). The workaround below should work for sure (the error you're getting is consistent to a process contents clause strict as opposed to lax).
Replace <xsd:element name="test" type="xsd:anyType" /> with a complex content that allows for xsd:any, like this:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="bookstore" type="bookstoreType"/>
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="test">
<xsd:complexType>
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Having "lax" will still yield a message; you could use "skip" if you want that message gone. Regardless, skip and lax in an xsd:any gives you what you need.

Categories

Resources