查询具有多个具有相同名称和不同属性值的元素的XML文档

时间:2015-07-22 19:11:54

标签: c# xml linq

我有一个XML Document,它有几个具有相同名称和不同属性值的元素,我正在查询从文档中获取数据到数组

 foreach (var trade in doc.Descendants("Trd"))
            {
                var RptSide = trade.Element("RptSide");
 sellerAccount = RptSide.Elements("Pty")
                        .Where(pty => pty.Attribute("Src").Value == "C")
                        .Select(pty => pty.Attribute("ID").Value)
                        .FirstOrDefault();
}

我的XML如下

<Trd>      
      <RptSide  Side="1">
        <Pty R="1" ID="666">
        </Pty>
        <Pty R="4" ID="666">
        </Pty>
        <Pty R="7" ID="ABC">
        </Pty>
        <Pty R="21" ID="CCC">
        </Pty>
        <Pty R="22" ID="NY">
        </Pty>
        **<Pty R="24" Src="C" ID="666ID">**
          <Sub Typ="26" ID="1">
          </Sub>
        </Pty>
        <Pty R="24" Src="H" ID="A-62370">
        </Pty>
        <Pty R="30" ID="apm">
        </Pty>
        <Pty R="36" ID="testausto">
          <Sub Typ="9" ID="Addo Ace">
          </Sub>
        </Pty>  
      </RptSide>
    </Trd>

从上面的XML中,我期待第ID个元素的Pty Attribute Src="C" 666ID sellerAccount存储在Src中,但它是仅考虑第一个没有FirstOrDefault()属性的Pty元素可能是因为An unhandled exception of type 'System.NullReferenceException' occurred in TestXM.exe Additional information: Object reference not set to an instance of an object. 的使用。

异常

sellerAccount = RptSide.Elements("Pty")
                        .Where(pty => pty.Attribute("R").Value == "24")
                        .Select(pty => pty.Attribute("ID").Value)
                       .FirstOrDefault();

但是当我将代码更改为下面时,它工作得很好

Pty

我是否知道查询文档以查找具有Src属性的class Test { static function main() { var sideeffects = 0; var cached = memoize(function (x) return x + sideeffects++); cached(1); trace(sideeffects);//1 cached(1); trace(sideeffects);//1 cached(3); trace(sideeffects);//2 cached(3); trace(sideeffects);//2 } @:generic static function memoize<In, Out>(f:In->Out):In->Out { var m = new Map<In, Out>(); return function (input:In) return switch m[input] { case null: m[input] = f(input); case output: output; } } } 元素的正确方法?

1 个答案:

答案 0 :(得分:1)

您只需要在Src上调用.Value之前检查NullReferenceException属性结果,以避免var sellerAccount = RptSide.Elements("Pty") .Where(pty => pty.Attribute("Src") != null && pty.Attribute("Src").Value.Equals("C")) .Select(pty => pty.Attribute("ID").Value) .FirstOrDefault();

例如,

using System.Xml.XPath;

或者,(使用var sellerAccount = doc.XPathSelectElement("//*[@Src='C']").Attribute("ID").Value; ),

   std::string moduleName     = "<full absolute path to DLL that this code is part of>";
   DWORD  zero                = 0;
   uint32 verInfoLen          = 0;
   BYTE   *verInfo            = NULL;
   VS_FIXEDFILEINFO *fileInfo = NULL;
   uint32 len                 = 0;

   /* Get the size of FileVersionInfo structure */
   verInfoLen = GetFileVersionInfoSize(moduleName.c_str(), &zero);
   if (verInfoLen == 0) {
      printf("GetFileVersionInfoSize() Failed!");
      return;
   }

   /* Get FileVersionInfo structure */
   verInfo = new BYTE[verInfoLen];
   if (!GetFileVersionInfo(moduleName.c_str(), zero, verInfoLen, verInfo)) {
      printf("GetFileVersionInfo Failed!");
      return;
   }

   /* Query for File version details. */
   if (!VerQueryValue(verInfo, "\\", (LPVOID *)&fileInfo, &len)) {
      printf("VerQueryValue Failed!");
      return;
   }

   /* None of the above func calls fail - but both printf below print all zeros */
   printf("Version is %d.%d.%d.%d",
                     (fileInfo->dwProductVersionMS >> 16) & 0xff,
                     (fileInfo->dwProductVersionMS >>  0) & 0xff,
                     (fileInfo->dwProductVersionLS >> 16) & 0xff,
                     (fileInfo->dwProductVersionLS >>  0) & 0xff);

   printf("Version is %d.%d.%d.%d",
                     HIWORD(fileInfo->dwProductVersionMS),
                     LOWORD(fileInfo->dwProductVersionMS),
                     HIWORD(fileInfo->dwProductVersionLS),
                     LOWORD(fileInfo->dwProductVersionLS));
相关问题