0

我正在尝试从我的 XML 文件中读取一些数据

姓名/姓名/trckid

  • 1)例如从下面粘贴的xml文件中我想读取名称/名称/trckid
    例如:trckid = FF7eb01f7-7985-47b1-8f8c-a0e92395d00f

  • 2) 以及 Names/Name/Files/file/uri 其中 formatCode="2001"
    例如结果字符串:uri=http://cont.catalog.address.xyz.com/e2/ds/9b660964.jpg

这是我的示例 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<Names tracking="trckids" execTime="0" xmlns="urn:schemas-microsoft-com:address:catalog">
    <Name xmlns="urn:schemas-microsoft-com:address:catalog" publishState="Published" lcid="1033" options="0" version="65" filterFlags="32687">
        <trckid ref="5ofol35" seoMetaData="/xyz/gpscatalog/">FF7eb01f7-7985-47b1-8f8c-a0e92395d00f</trckid>
        <providerId>Test0_2286326_304163829</providerId>
        <files>
            <file formatCode="2001" gpsFileId="9B660964-1626-466C-8359-F036506D8769" height="102" width="136" fileHash="4a4bcceb08b4ccdd16a958e8687c9588">
                <uri>http://cont.catalog.address.xyz.com/e2/ds/9b660964-1626-466c-8359-f036506d8769.jpg</uri>
            </file>
            <file formatCode="2007">
                <uri>http://img1.catalog.address.xyz.com/image.aspx?uuid=d7eb01f7-7985-47b1-8f8c-a0e92395d00f&amp;w=136&amp;h=102</uri>
            </file>
            <file formatCode="2200" gpsFileId="6C34F723-D798-4DA8-AEE4-0442C239D3A0" fileSize="311469" height="180" width="320"                                                                             sourceFileHash="6ea74770f622e3e0dc9cf5e229c7de7f">
                <uri>http://cont.catalog.address.xyz.com/e2/ds/0909a015-4b6a-4574-be63-c2facd527b9f_0.xml</uri>
            </file>
        </files>
    </Name>
   <Name xmlns="urn:schemas-microsoft-com:address:catalog" publishState="Published" lcid="1033" options="0" version="65" filterFlags="32687">
        <trckid ref="5ofol20" seoMetaData="/xyz/gpscatalog/">FF9eb01f7-7985-47b1-8f8c-a0e92395d00f</trckid>
        <providerId>Test1TheLorax_2286326_304163829</providerId>
        <files>
            <file formatCode="2001" gpsFileId="9B660964-1626-466C-8359-F036506D8769" height="102" width="136" fileHash="4a4bcceb08b4ccdd16a958e8687c9588">
                <uri>http://cont.catalog.address.xyz.com/e2/ds/9b660964-1626-466c-8359-f036506d8769.jpg</uri>
            </file>
            <file formatCode="2007">
                <uri>http://img1.catalog.address.xyz.com/image.aspx?uuid=d7eb01f7-7985-47b1-8f8c-a0e92395d00f&amp;w=136&amp;h=102</uri>
            </file>
            <file formatCode="2200" gpsFileId="6C34F723-D798-4DA8-AEE4-0442C239D3A0" fileSize="311469" height="180" width="320"                                                                             sourceFileHash="6ea74770f622e3e0dc9cf5e229c7de7f">
                <uri>http://cont.catalog.address.xyz.com/e2/ds/0909a015-4b6a-4574-be63-c2facd527b9f_0.xml</uri>
            </file>
        </files>
    </Name>
</Names>

我尝试使用以下给定的代码

      XDocument xdoc = XDocument.Parse(xmlstring);
            XNamespace ns = xdoc.Root.GetDefaultNamespace();  // "urn:schemas-microsoft-com:msnvideo:catalog";
            IEnumerable<XElement> names = xdoc.Descendants(ns + "name");
   foreach (XElement name in names)
            {


                 rssFeedItems.Add(new Adressses
                    {
                    trackid=  Convert.ToString(name.Element(ns + "trackid").Value),
                    providerID= Convert.ToString(name.Element(ns + "provideid").Value),

                    imageUri = Convert.ToString(name.Element(ns + "uri").Value)
                    });

            }        
  Here  am able to read first two items ,ie, trackid and providerID ,but not the image uri

我需要读取与 Names/Name/files/file@formatcode[2001]/uri 匹配的 uri

就像是

 imageUri = Convert.ToString(name.Element(ns + "Names/Name/files/file@formatcode[2001]/uri").Value)

但这只是行不通。请帮忙

4

3 回答 3

2

首先,您的 xml 无效。结束标记应如下所示,</Names>而不是<Names/>

其次,您必须添加命名空间声明。像这样:

    XNamespace cat = "urn:schemas-microsoft-com:address:catalog";
    XNamespace add = "urn:schemas-microsoft-com:msnvideo:address";
    XDocument doc = XDocument.Parse(xdata);
    var firstTrck = doc.Element(cat + "Names").Elements(add + "Name").First().Element(add + "trckid");
    var val = firstTrck.Value;

另外值得注意的是,注意如何在 Element/Elements 上附加 (s) 以确保您正在寻找一个或多个元素。

以下是迭代元素集合的方法:

        XNamespace cat = "urn:schemas-microsoft-com:address:catalog";
        XNamespace add = "urn:schemas-microsoft-com:msnvideo:address";
        XDocument doc = XDocument.Parse(xdata);

        var xNames = doc.Element(cat + "Names").Elements(add + "Name");
        foreach (var xName in xNames)
        {
            var trackingId = xName.Element(add + "trckid").Value;

            var xFiles = xName.Element(add + "files").Elements(add + "file").Where(xe => (string)xe.Attribute("formatCode") == "2001");
            foreach (var xFile in xFiles)
            {
                var uri = xFile.Element(add + "uri").Value;
            }
        }

请注意,当您对此进行调试时,只有一个“名称”元素出现在集合中。这是因为xmlns声明不匹配每个。

于 2012-04-27T00:56:05.380 回答
1

我认为这将满足您的需求。

XmlDocument doc = new XmlDocument();
WebClient webClient = new WebClient();
string xmlstring = webClient.DownloadString(url);
doc.LoadXml(xmlstring);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("ns", "urn:schemas-microsoft-com:address:catalog");
foreach (XmlNode n in doc.SelectNodes("/ns:Names/ns:Name/ns:trckid/@ref", nsMgr))
{
    Console.WriteLine(String.Format("trckid: {0}", n.InnerText));
}
foreach (XmlNode n in doc.SelectNodes("/ns:Names/ns:Name/ns:files/ns:file[@formatCode='2001']/ns:uri", nsMgr))
{
    Console.WriteLine(String.Format("uri: {0}", n.InnerText));
}
于 2012-04-27T02:47:11.963 回答
-1

为什么不将每一行读入一个字符串数组,然后搜索包含您要查找的前缀的行。剥去它的正面和背面并将其存储在一个变量中。这对你有用吗?

于 2012-04-27T00:22:24.320 回答