2012-04-05 59 views
1

這是我得到的012ml的輸出結果爲Infragistics ultrawebtree的xml。我正在使用它創建另一個具有相同結構的Infragistics ultrawebtree。但在這裏我不想要<Url>something.aspx..</Url>。我希望這是這樣的<Url><\Url>。所以我如何能夠刪除。這我得到作爲一個字符串。所以我用Regex.Replace()。但它會適用於某些條件,但在某些情況下,它會通過刪除一些xml標記來破壞xml,並且xml由於缺少標記而變得無效。 我用這個表達式<Url>\S*</Url>來避免Url的內容。任何幫助將非常有幫助。提前致謝。從以下XML中刪除節點。

<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
- <InfragisticsServerTree XmlVersion="1.0"> 
    - <uwtModule> 
    - <ProjectProperties> 
     <ComponentTarget>ClassicTree</ComponentTarget> 
     <BrowserTarget>Auto</BrowserTarget> 
    </ProjectProperties> 
    - <TreeProperties> 
     <MaxDataBindDepth>-1</MaxDataBindDepth> 
     <Name>uwtModule</Name> 
     <Indentation>20</Indentation> 
     <SubMenuImage>igNone</SubMenuImage> 
     <LoadOnDemandPrompt> 
     <b>Loading...</b> 
     </LoadOnDemandPrompt> 
     <ExpandAnimation>Decelerate</ExpandAnimation> 
     <ExpandOnClick>false</ExpandOnClick> 
     <CompactRendering>false</CompactRendering> 
     <RenderAnchors>false</RenderAnchors> 
     - <Style> 
     <ForeColor>-16777216</ForeColor> 
     <BorderColor>-4144960</BorderColor> 
     <BorderStyle>None</BorderStyle> 
     <BorderWidth>1px</BorderWidth> 
     - <Font> 
      <Name>Arial</Name> 
      - <Names> 
      <Name>Arial</Name> 
      </Names> 
      <Size>11px</Size> 
     </Font> 
     <Height>425px</Height> 
     <Width>97%</Width> 
     - <Padding> 
      <Top>5px</Top> 
      <Left>5px</Left> 
      <Right>5px</Right> 
      <Bottom>5px</Bottom> 
     </Padding> 
     </Style> 
     - <SelectedNodeStyle> 
     <BackColor>-2894893</BackColor> 
     <ForeColor>-16777216</ForeColor> 
     - <Padding> 
      <Top>2px</Top> 
      <Left>2px</Left> 
      <Right>2px</Right> 
      <Bottom>2px</Bottom> 
     </Padding> 
     </SelectedNodeStyle> 
    </TreeProperties> 
    <Styles /> 
    - <Levels> 
     - <Level> 
     <Number>0</Number> 
     </Level> 
    </Levels> 
- <Nodes> 
    - <Node> 
    <Text>123</Text> 
    <Url>ModuleEdit.aspx?ModuleID=965</Url> 
    <Target>main</Target> 
    <Tag>965</Tag> 
    <Title>AccptChangesPerfPM</Title> 
    <Expanded>true</Expanded> 
    - <Nodes> 
     - <Node> 
     <Text>111</Text> 
     <Url>123.aspx?e=965 </Url> 
     <Target>main</Target> 
     <Tag>TL_-99999</Tag> 
     </Node> 
     - <Node> 
     <Text>werrv</Text> 
     <Url>1dfee.aspx?qwe=9er65</Url> 
     <Target>main</Target> 
     <Tag>12DDfe</Tag> 
     </Node> 
     - <Node> 
     <Text>q2233</Text> 
     <Target>main</Target> 
     <Tag>TL_1015</Tag> 
     <Title>Topic_1</Title> 
     <ShowExpand>true</ShowExpand> 
     - <Nodes> 
      - <Node> 
      <Text>T1</Text> 
      <Url>w3345_954y65.aspx?ID=965er</Url> 
      <Target>main</Target> 
      - <Style> 
       <ForeColor>-16777216</ForeColor> 
      </Style> 
      <Tag>82355</Tag> 
      <Title>T1</Title> 
      </Node> 
      - <Node> 
      <Text>T2</Text> 
      <Url>23_7811.aspx?ID=3u65</Url> 
      <Target>main</Target> 
      - <Style> 
       <ForeColor>-16777216</ForeColor> 
      </Style> 
      <Tag>82356</Tag> 
      <Title>T2</Title> 
      </Node> 
      - <Node> 
      <Text>T3</Text> 
      <Url>we456_9.aspx?ID=4r56</Url> 
      <Target>main</Target> 
      - <Style> 
       <ForeColor>-16777216</ForeColor> 
      </Style> 
      <Tag>82357</Tag> 
      <Title>T3</Title> 
      </Node> 
     </Nodes> 
     </Node> 
    </Nodes> 
    </Node> 
</Nodes> 
    </uwtModule> 
</InfragisticsServerTree> 
+0

更改爲Xml類會更容易。 您是否考慮過這種替代方案? – 2012-04-05 14:37:14

+0

我試過Regex,xml到DataSet。但我不知道如何使用XMLDoc和XML。我也試過,但將會在下。 的子節點。那麼我將如何查詢? – kbvishnu 2012-04-05 14:39:58

回答

4

我的建議是忽略正則表達式。

Go對於VB的XML類來從文件中讀取, 然後與節點列表和節點一起工作。

這裏有一些鏈接,讓你開始:

Create XML in VB#

Modify XML literals in VB#

Samples of multiple actions on XML files in VB#

[VB.NET] XML - Create and Merge Files. Edit, Add and Delete Nodes.

在你上面的鏈接會發現重文檔和示例如何打開,編輯,創建,合併。

下面是一個如何做你所問以上︰ 但公正的警告:這是C#你需要使用上面的鏈接來轉換,因爲我不是很多的VB.Net開發人員

string path = "./"; //your own path 
string name = "Tempo"; //filename 
XmlDocument f = new XmlDocument(); 
f.Load(path + name + ".xml"); 
XmlNodeList a = f.GetElementsByTagName("Url"); 
for (int i = 0; i < a.Count; i++) 
{ 
    a[i].InnerText = "" //This had the data inside <Url>...</Url> 
} 
    f.Save(path+name); //Edited to add the save 
+0

感謝它的作品..我可以知道表演效果嗎? – kbvishnu 2012-04-05 16:19:33

+0

打開一個文件,將它加載到一個結構搜索它適應保存完整的文件從不是一個輕量級的操作,當XML很大時。 但我不認爲與正則表達式更簡單,但我沒有數據備份我的意見。 – 2012-04-05 16:36:42

+0

我不保存到文件。我正在用一個字符串處理它。 – kbvishnu 2012-04-05 16:47:17