2011-08-02 4 views
1

일부 생성 된 XML에 C14N 변환을 적용하려고합니다. 정규화를 수행하기 위해 LINQ를 사용하여 노드를 검색 할 수 없기 때문에 DOM을 사용하여 '구식'으로 이동해야하지만 기본 네임 스페이스의 파울로 떨어지는 것 같습니다.표준화를 위해 C#에서 기본 네임 스페이스로 Xpath 사용

다음은 내 코드 샘플입니다.

static void Main(string[] args) 
{ 
    XmlDocument xDoc = new XmlDocument(); 

    // Load some test xml 
    string path = @"..\..\TestFiles\Test_1.xml"; 
    if (File.Exists(path) == true) 
    { 
     xDoc.PreserveWhitespace = true; 
     using (FileStream fs = new FileStream(path, FileMode.Open)) 
     { 
      xDoc.Load(fs); 
     } 
    } 

    //Instantiate an XmlNamespaceManager object. 
    System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xDoc.NameTable); 

    //Add the namespaces used in books.xml to the XmlNamespaceManager. 
    xmlnsManager.AddNamespace("", "http://www.myApps.co.uk/"); 

    // Create a list of nodes to have the Canonical treatment 
     //Execute the XPath query using the SelectNodes method of the XmlDocument. 
     //Supply the XmlNamespaceManager as the nsmgr parameter. 
     //The matching nodes will be returned as an XmlNodeList. 
    XmlNodeList nodeList = xDoc.SelectNodes("/ApplicationsBatch/Applications|/ApplicationsBatch/Applications//*", xmlnsManager); 

    // Perform the C14N transform on the data 
    XmlDsigC14NTransform transform = new XmlDsigC14NTransform(); 

    transform.LoadInput(nodeList); 
    MemoryStream ms = (MemoryStream)transform.GetOutput(typeof(Stream)); 

    File.WriteAllBytes(@"..\..\TestFiles\ModifiedTest_1", ms.ToArray()); 
} 

그리고 내 XML : 나는이 지역 주변의 몇 가지 다른 주제를 읽고이 Gem을 통해 들어 왔지만이 문제를 해결 아니에요했습니다

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<ApplicationsBatch xmlns="http://www.myApps.co.uk/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <MessageHeader> 
    <MessageID>00000003</MessageID> 
    <Body>11223344556</Body> 
    <Timestamp>2011-08-02T09:00:00</Timestamp> 
    <MessageCheck>?</MessageCheck> 
    </MessageHeader> 
    <Applications> 
    <Application> 
     <ApplicantDetails> 
     <Title>MR</Title> 
     <Forename>HOMER</Forename> 
     <Middlenames> 
      <Middlename></Middlename> 
     </Middlenames> 
     <PresentSurname>SIMPSON</PresentSurname> 
     <CurrentAddress> 
      <Address> 
      <AddressLine1>ADDRESS LINE1</AddressLine1> 
      <AddressLine2>ADDRESS LINE2</AddressLine2> 
      <AddressTown>ADDRESS Town</AddressTown> 
      <AddressCounty>COUNTY</AddressCounty> 
      <Postcode>POST CODE</Postcode> 
      <CountryCode>GB</CountryCode> 
      </Address> 
      <ResidentFromGyearMonth>2007-01</ResidentFromGyearMonth> 
     </CurrentAddress> 
     </ApplicantDetails> 
    </Application> 
    <Application> 
     <ApplicantDetails> 
     <Title>MR</Title> 
     <Forename>BART</Forename> 
     <Middlenames> 
      <Middlename></Middlename> 
     </Middlenames> 
     <PresentSurname>SIMPSON</PresentSurname> 
     <CurrentAddress> 
      <Address> 
      <AddressLine1>ADDRESS LINE1</AddressLine1> 
      <AddressLine2>ADDRESS LINE2</AddressLine2> 
      <AddressTown>ADDRESS Town</AddressTown> 
      <AddressCounty>COUNTY</AddressCounty> 
      <Postcode>POST CODE</Postcode> 
      <CountryCode>GB</CountryCode> 
      </Address> 
      <ResidentFromGyearMonth>2007-01</ResidentFromGyearMonth> 
     </CurrentAddress> 
     </ApplicantDetails> 
    </Application> 
    </Applications> 
</ApplicationsBatch> 

.

XPath 시각화 도우미를 사용하면 필요한 노드를 선택해야하지만 코드에서 선택하지 못하는 것으로 나타납니다.

답변

0

나는 내 문제에 대한 부분적인 대답을 발견했다.

새 네임 스페이스가 관리자에 추가되면 기본 네임 스페이스가 빈 문자열이 될 수없는 것처럼 보입니다. 나는 다음과 같은 네임 스페이스 식별자를 반영하기 위해 XPath를 수정하는 데 필요한

//Instantiate an XmlNamespaceManager object. 
System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xDoc.NameTable); 

//Add the namespaces used to the XmlNamespaceManager. 
xmlnsManager.AddNamespace("x", "http://www.myApps.co.uk/"); 

: 이 내가 함께 결국 무엇

// Create a list of nodes to have the Canonical treatment 
    //Execute the XPath query using the SelectNodes method of the XmlDocument. 
    //Supply the XmlNamespaceManager as the nsmgr parameter. 
    //The matching nodes will be returned as an XmlNodeList. 
XmlNodeList nodeList = xDoc.SelectNodes("/x:ApplicationsBatch/x:Applications|/x:ApplicationsBatch/x:Applications//*", xmlnsManager); 

노드는 이제 선택 및 변환을위한 준비가 ... XML의 올바른 구조를 반환하지만 모든 값은 제거되었지만 이는 다른 질문에 대한 문제입니다.

+0

당신은 xpath와 함께 사용할 기본 네임 스페이스를 지정할 수 없다고 말하고자합니다. xpath는 여러 개의 접두어가있는 여러 노드에 걸쳐 있으므로 더 중요한 것은 xpath에서 기본 네임 스페이스처럼 보이는 null 네임 스페이스가있는 노드를 처리해야한다는 것입니다. XML 내에서 기본 네임 스페이스인지 널 네임 스페이스인지 판단하는 데 도움이되는 컨텍스트가 있습니다. –

관련 문제