XML节点不是熟悉的格式

时间:2013-06-05 17:57:32

标签: c# asp.net xml

我正在尝试从我的C#代码访问由第三方设置的xml。我可以通过使用定义良好的节点来访问XML,他们如何让我们不熟悉他们的XML设置。有没有办法可以获得这些数据?

以下是我不熟悉的XML示例。感谢。

<DATA>  
<HOTSPOTSETUP 
    CHOICELIST="2" 
    LISTSOURCE="NAME"
    LISTTITLE="Hotspots"
    INITIALVISIBILITY="1" 
    MINSCALE="0.25"
    MAXSCALE="2" />
<HOTSPOT 
    ID="1"
    NAME="Simple Caption Hotspot"
    MEDIATYPE="url"
    MEDIA="Assets/Hotspots/hotspotFromJPG.jpg"
    X="225"
    Y="1375"
    ZOOM="100" 
    XSCALE="100"
    YSCALE="100"
    URL="http://www.zoomify.com"
    URLTARGET="_self"
    ROLLOVER="0"
    CAPTION="Simple Caption"
    TOOLTIP="This is a simple tooltip." >
</HOTSPOT>
<HOTSPOT 
    ID="2"
    NAME="Transparent PNG Hotspot"
    MEDIATYPE="url"
    MEDIA="Assets/Hotspots/hotspotFromPNG.png"
    X="525"
    Y="1375"
    ZOOM="100" 
    XSCALE="100"
    YSCALE="100"
    URL="http://www.viewthewall.com"
    URLTARGET="_blank"
    ROLLOVER="0"
    CAPTION="Transparent PNG Hotspot"
    TOOLTIP="This hotspot's graphic includes transparent areas." >
</HOTSPOT>
<HOTSPOT 
    ID="3"
    NAME="Hotspot Without Caption"
    MEDIATYPE="url"
    MEDIA="Assets/Hotspots/hotspotFromPNG.png"
    X="825"
    Y="1375"
    ZOOM="100" 
    XSCALE="100"
    YSCALE="100"
    URL="http://www.adobe.com"
    URLTARGET="_blank"
    ROLLOVER="0"
    CAPTION=""
    TOOLTIP="This hotspot has no caption." >
</HOTSPOT>
<HOTSPOT 
    ID="4"
    NAME="Hotspot Visible On Rollover"
    MEDIATYPE="url"
    MEDIA="Assets/Hotspots/hotspotFromPNG.png"
    X="1125"
    Y="1375"
    ZOOM="100" 
    XSCALE="100"
    YSCALE="100"
    URL="http://www.zoomify.com"
    URLTARGET="_blank"
    ROLLOVER="1"
    CAPTION="Rollover Hotspot"
    TOOLTIP="This hotspot is revealed on mouse-over." >
</HOTSPOT>
<HOTSPOT 
    ID="5"
    NAME="Hotspot Without Graphic"
    MEDIATYPE="url"
    MEDIA=""
    X="1125"
    Y="1500"
    ZOOM="100" 
    XSCALE="100"
    YSCALE="100"
    URL="http://www.zoomify.com"
    URLTARGET="_blank"
    ROLLOVER="0"
    CAPTION="Hotspot Without Graphic (roll mouse over ruby above!)"
    TOOLTIP="This hotspot has no graphic." >
</HOTSPOT>
<HOTSPOT 
    ID="6"
    NAME="Hotspot Without Click Link"
    MEDIATYPE="url"
    MEDIA="Assets/Hotspots/hotspotFromPNG.png"
    X="1425"
    Y="1375"
    ZOOM="100" 
    XSCALE="100"
    YSCALE="100"
    URL=""
    URLTARGET=""
    ROLLOVER="0"
    CAPTION="Hotspot Without Click Link Or Tooltip"
    TOOLTIP="" >
</HOTSPOT>
<HOTSPOT 
    ID="7"
    NAME="Reused External Icon"
    MEDIATYPE="url"
    MEDIA="Assets/Hotspots/hotspotFromJPG.jpg"
    X="1725"
    Y="1375"
    ZOOM="100" 
    XSCALE="100"
    YSCALE="100"
    URL="http://www.adobe.com"
    URLTARGET="_self"
    ROLLOVER="0"
    CAPTION="Reusing External Icons is OK"
    TOOLTIP="This hotspot automatically clones the JPG graphic file." >
</HOTSPOT>
</DATA>

6 个答案:

答案 0 :(得分:2)

它们是属性:使用Linq2Xml,您可以轻松解析它。

var xDoc = XDocument.Load(filename);

var result = xDoc.Descendants("HOTSPOT")
                .Select(h => new
                {
                    Name = (string)h.Attribute("NAME"),
                    Media = (string)h.Attribute("MEDIA"),
                    X = (int)h.Attribute("X"),
                    // .......
                })
                .ToList();

答案 1 :(得分:1)

这些是属性值。用“@”查询如下:

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(text);
Console.WriteLine(xdoc.SelectSingleNode("/DATA/HOTSPOTSETUP/@LISTTITLE").Value);

答案 2 :(得分:1)

你可以这样做:

        XmlDocument doc = new XmlDocument();
        try { doc.Load("c:\\temp\\test.xml"); }
        catch (Exception ex) { }
        XmlElement root = doc.DocumentElement;

        foreach (XmlNode node in root.SelectNodes("/DATA"))  //could eliminate this outer loop if only one "DATA" block exists (adjust the other XPath to compensate)
        {
            foreach (XmlNode child in node.ChildNodes)
            {
                String attrValue = child.Attributes["XSCALE"].Value;
            }
        }

答案 3 :(得分:0)

foreach (XmlElement hotspot in doc.SelectNodes("//HOTSPOT"))
{
    foreach (XmlAttribute attribute in hotspot.Attributes)
        Console.WriteLine("{0}={1}", attribute.Name, attribute.Value);

    Console.WriteLine("");
}

顺便说一下,如果你找一个XPath教程,你会发现你的任务更容易。

答案 4 :(得分:0)

XML文件看起来很好。使用LINQ to SQL来读取它

string Path = "YourURLorFileLocation";
XDocument doc = XDocument.Load(Path);

//To read the HOTSPOT elements
var HotSpotElements = doc.Element("DATA").Elements("HOTSPOT");
//OR you can use
//var HotSpotElements = doc.Descendants("HOTSPOT")
//Element(s) looks at immediate child while Descendants look at all children

foreach (var element in HotSpotElements)
{
    //This is how you will read the attributes
    var ID = element.Attribute("ID").Value;                    
}

答案 5 :(得分:0)

XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath("~/hotspots.xml"));

    XmlNode root = doc.DocumentElement;

    XmlNodeList nodeList = root.SelectNodes("HOTSPOT");

    foreach (XmlNode node in nodeList)
    {
        XmlNode idNode = node.SelectSingleNode("@ID");
        //nodeText = node.SelectSingleNode("ID").ToString();
        if(idNode != null)
        {
            ddlXml.Items.Add("Item - " + idNode.InnerText);
        }

    }