2015-01-04 5 views
0

아마존 API에서 xml 응답을 구문 분석하려고합니다. 내가 인수 타임 스탬프를 읽을 수Amzon API 구문 분석 * .xml 응답

<BrowseNodeLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"> 
    <OperationRequest> 
    <RequestId>31317fca-ad3d-4ff0-a64f-693c0e44959b</RequestId> 
    <Arguments> 
     <Argument Name="Operation" Value="BrowseNodeLookup" /> 
     <Argument Name="Service" Value="AWSECommerceService" /> 
     <Argument Name="Version" Value="2011-08-01" /> 
     <Argument Name="BrowseNodeId" Value="186606" /> 
     <Argument Name="Timestamp" Value="2015-01-04T11:50:06Z" /> 
     <Argument Name="ResponseGroup" Value="BrowseNodeInfo" /> 
    </Arguments> 
    <RequestProcessingTime>0.002221</RequestProcessingTime> 
    </OperationRequest> 
    <BrowseNodes> 

:

는 수신 된 XML 파일의 일부이다. 이것은 내 코드이지만 XML 파일에서 xmlns 특성을 제거한 경우에만 작동합니다.

Dim nodeTimestamp As XmlNode = doc.SelectSingleNode("/BrowseNodeLookupResponse/OperationRequest/Arguments/Argument[@Name='Timestamp']") 

    Dim text As String = nodeTimestamp.Attributes.ItemOf("Value").InnerText 
+0

자주 묻는 질문입니다. 대답은'XmlNamespaceManager'를 사용하고 아마존 네임 스페이스의 접두어를 선언하는 것입니다. – Tomalak

답변

0

난 당신이 아마존 (마지막 닫는 태그가 </BrowseNodeLookupResponse>이다에서 유효한 XML을받을 가정합니다. 당신이 원하는 값 속성의 값을 선택하기 위해 LINQ2XML를 사용할 수 있습니다. 당신은 기본 네임 스페이스를 받아 사용합니다 .

' load the xml. Use XDocument.Parse to load a XML-Structure from a String! 
Dim xml as XDocument = XDocument.Load("c:\temp\axml.xml") 
' get the namespace for later use 
Dim ns = xml.Root.GetDefaultNamespace() 
' find the Arguments node, holding all Argument nodes. Note the use of the namespace 
Dim arguments = xml.Root _ 
    .Descendants(ns + "Arguments") _ 
    .Elements(ns + "Argument") 
' find and take the Argument node with the specified name 
Dim ts = from argument in arguments 
     where argument.Attribute("Name").Value = "Timestamp" 
     select argument 
' take the value of the desired attribute 
Console.WriteLine(ts.FirstOrDefault().Attribute("Value").Value) 

출력은 다음과 같습니다 :

2015-01-04T11:50:06Z 
액세스하고 XML의 특정 요소/노드를 검색 할 때 예를 들어 나는 axml.xml라는 파일에 XML 입력을 저장 한3210

XmlDocument 방식을 계속 사용하려면 XmlDocumentNamespaceManager (as shown in this SO post)을 사용하여 네임 스페이스를 사용하여 노드를 검색해야합니다. 해결책은 다음과 같습니다.

Dim xml = new XmlDocument() 
xml.Load("c:\temp\axml.xml") 
Dim xmlnsManager = new XmlNamespaceManager(xml.NameTable) 
' take the custom namespace and add it to the namespace manager 
xmlnsManager.AddNamespace("custom", xml.ChildNodes(0).Attributes("xmlns").Value) 
' find the desired node 
Dim nodeTimestamp = xml.SelectSingleNode("//custom:Arguments/custom:Argument[@Name='Timestamp']", xmlnsManager) 
' and take the attribute value 
Console.WriteLine(nodeTimestamp.Attributes("Value").Value) 

출력은 위와 같습니다.

+0

답변 해 주셔서 감사합니다. 나는 Linq과 함께 시도 할 것이다. 왜냐하면 나는 내 프로젝트에서 다른 것들에도 사용하기 때문이다. 나는 amazon에서 데모 코드를 본 이후로 네임 스페이스 매니저의 감각을 궁금해합니다. 이제는 내 코드가 작동하는 네임 스페이스 매니저와 함께 알았습니다. – Jimmy09

+0

예제에서와 같이 XmlDocument를 사용하는 솔루션을 추가했습니다. – pasty

+0

@Tomalak 발언 해 주셔서 감사합니다. - 당신은 엄밀합니다. 나는 PushScope를 제거했습니다. 빈 접두어에 대해서 - 작동하지 않았습니다. – pasty