2012-10-08 3 views
8

का उपयोग कर एक्सएमएल/केएमएल फाइलों में पढ़ना मैं एक आवेदन में kml xml Google धरती फ़ाइल आयात करने की कोशिश कर रहा हूं, लेकिन मुझे ऐसा करने के लिए xDocument सिंटैक्स सही नहीं लगता है, मैं चाहता हूं सोच रहा है कि कोई भी kml xml फ़ाइल में पढ़ने का तरीका सुझा सकता है।सी #

मैं एक्सएमएल आयात की मूल बातें समझ लेकिन xDocument और Linq के साथ काम करने के लिए कुछ भी नहीं मिल सकता है, आदर्श मैं प्रत्येक स्थान-चिह्न एक वस्तु के रूप हो जाते हैं और उन्हें अपने इकाई की रूपरेखा संचालित डाटाबेस को जोड़ना चाहते हैं। मुझे यह सुझाव देना चाहिए कि मुझे यह कैसे करना चाहिए, क्योंकि मैं सिर्फ लिंक के साथ शुरू कर रहा हूं और कुछ पॉइंटर्स के साथ कर सकता हूं। एक्सएमएल

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://earth.google.com/kml/2.2"> 
    <Document> 
    <Placemark> 
     <name>XXX</name> 
     <description>XXX</description> 
     <styleUrl>XXX</styleUrl> 
     <Point> 
     <coordinates>XXX</coordinates> 
     </Point> 
    </Placemark> 
    <Placemark> 
     <name>XXX</name> 
     <description>XXX</description> 
     <styleUrl>XXX</styleUrl> 
     <Point> 
     <coordinates>XXX</coordinates> 
     </Point> 
    </Placemark> 
    </Document> 
</kml> 
+0

आपके पास कोई कोड नहीं है, कृपया अगली बार कोड शामिल करना न भूलें। – Guvante

उत्तर

7

आपने कोई कोड शामिल नहीं किया है, लेकिन मुझे लगता है कि आप चीजों का संदर्भ देते समय अपना नामस्थान शामिल करना भूल गए हैं। यहाँ एक उदाहरण है।

मूल पहुंच:

var placemarks = xdoc.Element("kml").Element("Document").Elements("Placemark"); 

का उपयोग नामस्थान:

var ns = XNamespace.Get("http://earth.google.com/kml/2.2"); 
var placemarks = xdoc.Element(ns + "kml").Element(ns + "Document").Elements(ns + "Placemark"); 
+0

निहित रूपांतरण की बजाय 'गेट' विधि का उपयोग करने का कोई कारण? –

+0

@ जोन्सस्केट: मैं हमेशा निहित रूपांतरण के बारे में भूल जाता हूं क्योंकि मैंने पहली बार लिंक से एक्सएमएल में एफ # में काम किया था। मेरा अधिकांश कोड समान नामस्थान का उपयोग करता है, इसलिए मैं परिभाषाओं के चारों ओर प्रतिलिपि बनाता हूं। – Guvante

5

नीचे दी गई मेरी अनुमान के रूप में प्रदर्शित किया गया है कि आप XML प्रश्नों के अपने LINQ में नाम स्थान का उपयोग करने के भूल गए है। यह आसान इस से डेटा निकालने के लिए पर्याप्त है:

XNamespace ns = "http://earth.google.com/kml/2.2"; 
var doc = XDocument.Load("file.xml"); 
var query = doc.Root 
       .Element(ns + "Document") 
       .Elements(ns + "Placemark") 
       .Select(x => new PlaceMark // I assume you've already got this 
         { 
          Name = x.Element(ns + "name").Value, 
          Description = x.Element(ns + "description").Value, 
          // etc 
         }); 

अगर वह मदद नहीं करता है, तो आप क्या कोशिश की है की एक पूरी उदाहरण पोस्ट करें, और क्या गलत हो गया।

2
var xDoc = XDocument.Load("a.xml"); 
XNamespace ns = "http://earth.google.com/kml/2.2"; 

var placemarks = xDoc.Descendants(ns+"Placemark") 
        .Select(p => new 
        { 
         Name = p.Element(ns+"name").Value, 
         Desc = p.Element(ns+"description").Value 
        }) 
        .ToList(); 
3

मैं SharmpKml और उसके documentation इस्तेमाल किया KML फ़ाइल से जानकारी निकालने के लिए।

using SharpKml.Dom; 
using SharpKml.Engine; 
using SharpKml.Dom.GX; 

TextReader reader = File.OpenText(filePath); 
KmlFile file = KmlFile.Load(reader); 
_kml = file.Root as Kml; 

sPlaceMarks[] tempPlaceMarks = new sPlaceMarks[1000]; 
if (_kml != null) 
{ 
    foreach (var placemark in _kml.Flatten().OfType<Placemark>()) 
    { 
    tempPlaceMarks[numOfPlaceMarks].Name = placemark.Name; 
    tempPlaceMarks[numOfPlaceMarks].Description = placemark.Description.Text; 
    tempPlaceMarks[numOfPlaceMarks].StyleUrl = placemark.StyleUrl; 
    tempPlaceMarks[numOfPlaceMarks].point = placemark.Geometry as SharpKml.Dom.Point; 
    tempPlaceMarks[numOfPlaceMarks].CoordinateX = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Longitude; 
    tempPlaceMarks[numOfPlaceMarks].CoordinateY = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Latitude; 
    tempPlaceMarks[numOfPlaceMarks].CoordinateZ = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Altitude; 
    numOfPlaceMarks++; 
    } 

    foreach (var lookAt in _kml.Flatten().OfType<LookAt>()) 
    { 
    Placemark placemark = (Placemark)lookAt.Parent; 
    for (int i = 0; i < numOfPlaceMarks; i++) 
    { 
    if (placemark.Name == tempPlaceMarks[i].Name) 
    { 
     tempPlaceMarks[i].Name = placemark.Name; 
     tempPlaceMarks[i].Description = placemark.Description.Text; 
     tempPlaceMarks[i].StyleUrl = placemark.StyleUrl; 
     tempPlaceMarks[i].altitude = lookAt.Altitude; 
     tempPlaceMarks[i].AltitudeMode =(SharpKml.Dom.GX.AltitudeMode)lookAt.GXAltitudeMode; 
     tempPlaceMarks[i].Heading = lookAt.Heading; 
     tempPlaceMarks[i].Latitude = lookAt.Latitude; 
     tempPlaceMarks[i].Longitude = lookAt.Longitude; 
     tempPlaceMarks[i].Range = lookAt.Range; 
     tempPlaceMarks[i].Tilt = lookAt.Tilt; 
     break; 
    } 
    } 
}