我陷入了一些 LINQ to XML 代码 (
发布于 2024-12-19 01:26:32 字数 2713 浏览 1 评论 0 原文

我陷入了一些 LINQ to XML 代码的困境。在 for 循环中调用一个方法,该方法成功返回 标记的内容,该标记包含来自具有 4000+ 个 kml 文件的正确 id 的总数。我说成功了,但有一点需要注意。 标记包含 ,.net 将其转换为 .无论如何,我无法弄清楚如何对其进行解析,以便保留所有字符(>)。以下是相关的 C# 代码:

…in main class

public partial class ResultsGeneral : BasePage {
public List<string> rightVenues = new List<string>();
XNamespace spc = "http://www.opengis.net/kml/2.2";
XDocument Placemarks = XDocument.Load(HttpContext.Current.Server.MapPath("~/venuesCompleteGood.kml"));

…in protected void Page_Load(object sender, EventArgs e) {

for (int q = 0; q <= rightVenues.Count; q++) {
    getInfo(rightVenues[q]); //get info contains a list of placemark ids
};

public void getInfo(string ids) {
    var aPlacemark =
    from plMark in Placemarks.Descendants(spc + "Placemark")
        where plMark.Attribute("id").Value.Equals(ids.ToString())
        select plMark;
    foreach (XElement plMark in aPlacemark) {
        quKML.Text = quKML.Text + "<br>" + 
               plMark.ToString(SaveOptions.DisableFormatting);
    }
}

…kml 的片段:

<?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2">
        <Document>
            <Placemark id="33">
               <name>
                    Madison Square Garden
               </name>
               <description>
                    <![CDATA[<br>
4 Penn Plaza<br>
<br>
New York<br>
NY<br>
10001<br>
United States of America ]]>
               </description>
               <Point>
                    <coordinates>
                         -73.99337100000002,40.750354
                    </coordinates>
               </Point>
          </Placemark>

…返回的部分内容:

<placemark id="33" xmlns="http://www.opengis.net/kml/2.2"><name>
                    Madison Square Garden
               </name><description><!--[CDATA[<br-->
4 Penn Plaza<br>
<br>
New York<br>
NY<br>
10001<br>
United States of America ]]&gt;</description><point><coordinates>
                         -73.99337100000002,40.750354
                    </coordinates></point></placemark>

注意 ]]> 仅次于美国。

I’m stuck on some LINQ to XML code. A method is called in a for loop that successfully returns the contents of a <placemark> tag containing the correct <placemark> id from a kml file that has 4000+ <placemark>’s total. I say successfully with one caveat. The <description> tag contains <![CDATA[ ]]> that .net translates into <![CDATA[ ]] > . I can’t figure out anyway of getting this parsed so that it preserves all of the characters (the >). Here is the relevant c# code:

…in main class

public partial class ResultsGeneral : BasePage {
public List<string> rightVenues = new List<string>();
XNamespace spc = "http://www.opengis.net/kml/2.2";
XDocument Placemarks = XDocument.Load(HttpContext.Current.Server.MapPath("~/venuesCompleteGood.kml"));

…in protected void Page_Load(object sender, EventArgs e) {

for (int q = 0; q <= rightVenues.Count; q++) {
    getInfo(rightVenues[q]); //get info contains a list of placemark ids
};

public void getInfo(string ids) {
    var aPlacemark =
    from plMark in Placemarks.Descendants(spc + "Placemark")
        where plMark.Attribute("id").Value.Equals(ids.ToString())
        select plMark;
    foreach (XElement plMark in aPlacemark) {
        quKML.Text = quKML.Text + "<br>" + 
               plMark.ToString(SaveOptions.DisableFormatting);
    }
}

…a snippet of the kml:

<?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2">
        <Document>
            <Placemark id="33">
               <name>
                    Madison Square Garden
               </name>
               <description>
                    <![CDATA[<br>
4 Penn Plaza<br>
<br>
New York<br>
NY<br>
10001<br>
United States of America ]]>
               </description>
               <Point>
                    <coordinates>
                         -73.99337100000002,40.750354
                    </coordinates>
               </Point>
          </Placemark>

…part of what is returned:

<placemark id="33" xmlns="http://www.opengis.net/kml/2.2"><name>
                    Madison Square Garden
               </name><description><!--[CDATA[<br-->
4 Penn Plaza<br>
<br>
New York<br>
NY<br>
10001<br>
United States of America ]]></description><point><coordinates>
                         -73.99337100000002,40.750354
                    </coordinates></point></placemark>

note the ]]> after America.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2024-12-26 01:26:32

当我运行你的 getInfo 方法时,看起来 plMark.ToString(...) 返回正确的文本,所以我将调查 quKML,我可能会丢失一些东西,但我看不到这是什么数据类型?

此外,如果 quKML 应该是基于 XML 的,我会考虑将您的
更改为自闭标记

希望这有帮助。

When I run your getInfo method, it looks like plMark.ToString(...) is returning the right text, so I would be investigating quKML, I might be missing something, but I can't see what data type this is?

In addition, if quKML is supposed to be XML based, I'd think about altering your <br> to a self closing tag <br/>.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文