-1

我正在尝试从我的 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>
4

6 回答 6

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();
于 2013-06-05T18:03:56.033 回答
1

这些是属性值。像这样用“@”查询:

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(text);
Console.WriteLine(xdoc.SelectSingleNode("/DATA/HOTSPOTSETUP/@LISTTITLE").Value);
于 2013-06-05T18:01:24.770 回答
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;
            }
        }
于 2013-06-05T18:04:09.220 回答
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);
        }

    }
于 2013-06-05T18:47:40.200 回答
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 教程,您会发现您的任务更容易。

于 2013-06-05T18:07:39.100 回答
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;                    
}
于 2013-06-05T18:07:39.390 回答