Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C和VS2008将linq转换为xml_C#_.net_Xml_Linq - Fatal编程技术网

C# 使用C和VS2008将linq转换为xml

C# 使用C和VS2008将linq转换为xml,c#,.net,xml,linq,C#,.net,Xml,Linq,XML格式 <status> <webresponse> <properties> <props> <imghandle> <image handle=1234> <imagetag>somewhere</imagetag> </image> </imghandle> </props> <

XML格式

<status>
 <webresponse>
  <properties>
   <props>
    <imghandle>
     <image handle=1234>
      <imagetag>somewhere</imagetag>
     </image>
    </imghandle>
   </props>
   <props>
    <imghandle>
     <image handle=1235>
      <imagetag>somewhere1</imagetag>
     </image>
    </imghandle>
   </props>
  </properties>
 </webresponse>
</status>
PS:我知道我的xml格式不好,不便于阅读,在这里提问时如何放置xml部分

更新1: 所以在激怒了第三方之后,我终于得到了一个答案,说我使用的数据已经过时了,他们给了我新的格式化数据,看起来像这样

<status>
     <webresponse>
      <properties>
       <props>
        <imghandle>
         <image handle="1234">
          <imagetag>somewhere</imagetag>
         </image>
        </imghandle>
        <owner>
          <image handle="ABcf">
          </image>
        </owner>
       </props>
       </properties>
     </webresponse>
    </status>
选择c.Elementimghandle.Elementimage.Attributehandle

和/或

from c in searchXMLResult.Descendants("props")
选择c.Elementowner.Elementimage.Attributehandle

更新2: 这是我正在获取并保存到磁盘的xml

<?xml version="1.0" encoding="utf-8"?>
<status>
  <webresponse>
    <properties>
      <props>
        <imghandle>
          <image handle="537">
            <imagetag>SFO</imagetag>
          </image>
        </imghandle>
        <owner>
          <image handle="User-2">
            <firstname>
            </firstname>
            <lastname>Site Administrator</lastname>
            <username>admin</username>
          </image>
        </owner>
        <creationdate>2009-03-06T18:07:57Z</creationdate>
        <summary>
        </summary>
      </props>
      <status>HTTP/1.1 200 OK</status>
    </properties>
  </webresponse>
</status>

在“handle”属性值周围加上引号后,下面的代码对我很有用:

XDocument xdoc = XDocument.Parse(@"
<status>
 <webresponse>
  <properties>
   <props>
    <imghandle>
     <image handle='1234'>
      <imagetag>somewhere</imagetag>
     </image>
    </imghandle>
   </props>
   <props>
    <imghandle>
     <image handle='1235'>
      <imagetag>somewhere1</imagetag>
     </image>
    </imghandle>
   </props>
  </properties>
 </webresponse>
</status>
");

var v = from c in xdoc.Descendants("props")
select c.Element("imghandle").Element("image").Attribute("handle");
编辑:如果不修改输入以使xml有效,我不知道如何处理XDocument或XmlDocument对象。如果您试图加载无效的xml,这两种方法都会引发异常


EDIT2:第二个xml示例仍然无效:。标记未关闭

我继续并格式化了XML。最好的方法是选择要格式化的范围,然后按CTRL+K键。这很好,但OP似乎无法控制输入。有没有不修改输入的方法?属性值需要用单引号括起来,还是用双引号就足够了。此外,我对即将到来的数据没有控制权me@Whatsit-说得好。嗯,如果没有单引号或双引号,它就不是有效的xml。我不确定使用XDocument处理无效xml的选项是什么;var v=从xdoc.DegenantsProps中的c选择c.Elementimghandle.Elementimage;其中searchresult的类型为XMLDocument。还是给了我一个错误。我确实在属性值周围加了双引号,让第三方为我提供正确的xml就像转到DMVWhat error?XDocument.Parse行是失败的还是Linq语句?
<?xml version="1.0" encoding="utf-8"?>
<status>
  <webresponse>
    <properties>
      <props>
        <imghandle>
          <image handle="537">
            <imagetag>SFO</imagetag>
          </image>
        </imghandle>
        <owner>
          <image handle="User-2">
            <firstname>
            </firstname>
            <lastname>Site Administrator</lastname>
            <username>admin</username>
          </image>
        </owner>
        <creationdate>2009-03-06T18:07:57Z</creationdate>
        <summary>
        </summary>
      </props>
      <status>HTTP/1.1 200 OK</status>
    </properties>
  </webresponse>
</status>
XDocument xdoc = XDocument.Parse(@"
<status>
 <webresponse>
  <properties>
   <props>
    <imghandle>
     <image handle='1234'>
      <imagetag>somewhere</imagetag>
     </image>
    </imghandle>
   </props>
   <props>
    <imghandle>
     <image handle='1235'>
      <imagetag>somewhere1</imagetag>
     </image>
    </imghandle>
   </props>
  </properties>
 </webresponse>
</status>
");

var v = from c in xdoc.Descendants("props")
select c.Element("imghandle").Element("image").Attribute("handle");