本文介绍了Non Validatiing Parser(新手)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.NET的新手,也是c#中XML的处理,请原谅我的问题。我试图搜索类似的问题,但没有找到主导。

I'm new to .NET and the processing of XML in c#, so forgive me for my question. I tried to search for simular questions, but did not find a lead.

我想解析GPX(XML)文件。 Thos GPX文件是使用各种工具生成的,所以我没有说明这些文件是如何生成的,并且通常它们不会没有一些解析错误(如果我在XmlSpy中检查它们)。我希望我的解析器尽可能地作为子弹证明,所以我想在可能的情况下忽略架构解析错误。

I want to parse GPX (XML) files. Thos GPX files are generated with all kind of tools, so I have nothing to say how these are generated and often they are not without some parsing errors (if I check them within XmlSpy). I want my parser to be as bullet proof as possible, so I want to ignore schema parsing errors if possible.

我需要那些GPX文件中的轨道,可以由/获取/ trk xpath表达式。我使用以下代码snippit加载GPX:

I need the tracks from those GPX files, that could be fetched by the //trk xpath expression. I used the following code snippit to load the GPX:

FileStream fs = new FileStream(gpxFile,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
StreamReader reader = new StreamReader(fs);
XmlReaderSettings settings = new XmlReaderSettings();

FileStream fs = new FileStream(gpxFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader(fs);
XmlReaderSettings settings = new XmlReaderSettings();

//需要,因为GPX文件通常包含特殊字符,例如ü等。
// settings.CheckCharacters = false;

// Needed because the GPX files often contain special characters, like ü, etc.
//settings.CheckCharacters = false;

settings.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None;
settings.ValidationFlags | = XmlSchemaValidationFlags.ReportValidationWarnings;

settings.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

settings.ValidationEventHandler + = new ValidationEventHandler(ValidationCallBack);
settings.ValidationType = ValidationType.None;

settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
settings.ValidationType = ValidationType.None;

XmlReader xmlReader = XmlTextReader.Create(reader ,设置);

XmlReader xmlReader = XmlTextReader.Create(reader, settings);

_xmldoc = new XmlDocument();
_xmldoc.Load(xmlReader);

_xmldoc = new XmlDocument();
_xmldoc.Load(xmlReader);

_nsmgr = new XmlNamespaceManager(_xmldoc .NameTable);
_nsmgr.AddNamespace(" g"," http://www.topografix.com/GPX/1/1 ");

_nsmgr = new XmlNamespaceManager(_xmldoc.NameTable);
_nsmgr.AddNamespace("g", "http://www.topografix.com/GPX/1/1");


现在一个GPX我得到了正确的结果(nodes.Count> 0):


Now for one GPX I get the correct result with (nodes.Count > 0):


XmlElement root = _xmldoc.DocumentElement;
XmlNodeList节点= root.SelectNodes(" // g:trk",_ nsmgr);


XmlElement root = _xmldoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//g:trk", _nsmgr);

以下是有效GPX的示例:

Here is a sample of the valid GPX:


<?xml version =" 1.0"编码= QUOT; UTF-8英寸独立= QUOT;否] ?>
< gpx xmlns =" "创作者= QUOT;"版本= QUOT; 1.1" xmlns:xsi =" " xsi:schemaLocation =" http://www.topografix.com/GPX/1/1/gpx.xsd ">
< trk>
< name> ACTIVE LOG085012< / name>
< trkseg>
< trkpt lat =" 52.351241" LON = QUOT; 4.891165">
< ELE> -27.839< / ELE>
<时间> 2008-10-23T06:50:12Z< /时间>
< ; / trkpt>
< trkpt lat =" 52.351241" LON = QUOT; 4.891165">
< ELE> -28.489< / ELE>
<时间> 2008-10-23T06:50:17Z< /时间>
< ; / trkpt>
< / trkseg>
< / trk>
< / gpx>


<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<trk>
<name>ACTIVE LOG085012</name>
<trkseg>
<trkpt lat="52.351241" lon="4.891165">
<ele>-27.839</ele>
<time>2008-10-23T06:50:12Z</time>
</trkpt>
<trkpt lat="52.351241" lon="4.891165">
<ele>-28.489</ele>
<time>2008-10-23T06:50:17Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>

对于以下GPX我没有结果,除非我替换< gpx>中的命名空间和架构属性。元件。下面是无效的GPX(XmlSpy进行了成功的模式检查):

And for the following GPX I got no results, except when I replace the namespace and schema attributes in the <gpx> element. Here follows the invalid GPX (XmlSpy did a successfull schema check):

<?xml version =" 1.0" encoding =" UTF-8">
< gpx
version =" 1.0"
creator =" GPSBabel - http://www.gpsbabel.org "
xmlns:xsi =" xwns =" http://www.topografix.com/GPX/1/0 "
xsi:schemaLocation =" http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd ">
< trk>
< name> MTB-route Lombokbossen< /名称>
< number> 1< / number>
< trkseg>
<! - 开始路线1 - >
< trkpt lat =" 52.022776064" lon =" 5.430795075">
< ele> 21.766968< / ele>
< / trkpt>
< trkpt lat =" 52.023001621" lon =" 5.430844361">< ele> 21.766968< / ele>
< / trkpt>
< trkpt lat =" 52.023203373" LON = QUOT; 5.430857353">
< ELE> 21.286377< / ELE>
< / trkpt>
< / trkseg>
< / TRK>
< / gpx>

<?xml version="1.0" encoding="UTF-8"?>
<gpx
version="1.0"
creator="GPSBabel - http://www.gpsbabel.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
<trk>
<name>MTB-route Lombokbossen</name>
<number>1</number>
<trkseg>
<!-- begin route 1 -->
<trkpt lat="52.022776064" lon="5.430795075">
<ele>21.766968</ele>
</trkpt>
<trkpt lat="52.023001621" lon="5.430844361">
<ele>21.766968</ele>
</trkpt>
<trkpt lat="52.023203373" lon="5.430857353">
<ele>21.286377</ele>
</trkpt>
</trkseg>
</trk>
</gpx>


我做错了什么?我怀疑未经验证的解析器未打开,因为当我替换< gpx>时成功的GPX属性我得到了trk元素。我的验证回调中没有错误


What am I doing wrong? I suspect that the non-validated parsers is not turned on, because when I replace the <gpx> attributes with the GPX that succeeds I got the trk element. I got no errors in my validation callback

谢谢!

 

推荐答案


这篇关于Non Validatiing Parser(新手)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:05