14 Replies - 542 Views - Last Post: 27 December 2011 - 11:36 AM Rate Topic: -----

#1 malikahaki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-June 09

question about xml in c#

Posted 26 December 2011 - 03:12 AM

i want in the following file , first two lines changes two

<Letter>


namely <letter> is only and the first line is removed . how Do this in c# ?


this is my code:

<?xml version="1.0" encoding="utf-8"?>
<Letter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.irica.com/ECE/1383-12/SendSchema">
  <Protocol Name="ECE" Version="1.01" />
  <Software SoftwareDeveloper="dddddd" Version="2.3.2.9" GUID="2A43FD08-03B8-4297-898B-E7BD7F4D2E1B" />
  <Sender Organization="dddddd" Code="11111" />
  <Receiver Organization="dddddd" Code="2311" ReceiveType="Origin" />
  <OtherReceivers />
  <LetterNo>322/90/123</LetterNo>
  <LetterDateTime ShowAs="jalali">2011-12-20T15:53:15</LetterDateTime>
  <RelatedLetters />
  <Subject>dddddd</Subject>
  <Priority Name="dddddd" Code="0" />
  <Classification Name="dddddd" Code="1" />
  <Keywords />
  <Origins />
  <Attachments />
</Letter>



Is This A Good Question/Topic? 0
  • +

Replies To: question about xml in c#

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: question about xml in c#

Posted 26 December 2011 - 03:52 AM

** Moved into the C# Help forum. **
Please don't post questions in the Tutorials section.
Was This Post Helpful? 0
  • +
  • -

#3 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 75
  • Joined: 12-December 11

Re: question about xml in c#

Posted 26 December 2011 - 04:39 AM

Regex.Replace may be a decent place to start.
Was This Post Helpful? 0
  • +
  • -

#4 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: question about xml in c#

Posted 26 December 2011 - 04:59 AM

Regex.Replace only replaces some words, meaning you can replace the wanted word with a empty string, and then remove all empty strings from a file: read the file line by line and add it to a list of strings, if the read line is empty then don't add it. :D

Maybe take a look at the: TrimEnd Method (MSDN Article)

This post has been edited by RexGrammer: 26 December 2011 - 05:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 75
  • Joined: 12-December 11

Re: question about xml in c#

Posted 26 December 2011 - 05:35 AM

View PostRexGrammer, on 26 December 2011 - 04:59 AM, said:

Regex.Replace only replaces some words, meaning you can replace the wanted word with a empty string, and then remove all empty strings from a file: read the file line by line and add it to a list of strings, if the read line is empty then don't add it. :D

Maybe take a look at the: TrimEnd Method (MSDN Article)


Regex.Replace will replace whatever you define in your regular expression to replace, not just "some words". That's what he's trying to do.
Was This Post Helpful? 0
  • +
  • -

#6 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: question about xml in c#

Posted 26 December 2011 - 11:31 AM

My meaning was: it will replace the strings he wants with some other strings, it can not delete them ;)

This post has been edited by RexGrammer: 26 December 2011 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: question about xml in c#

Posted 26 December 2011 - 05:36 PM

It's xml treat as xml, not as a string.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#8 malikahaki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-June 09

Re: question about xml in c#

Posted 26 December 2011 - 11:09 PM

View Postmalikahaki, on 26 December 2011 - 11:02 PM, said:

View PostAdamSpeight2008, on 26 December 2011 - 05:36 PM, said:

It's xml treat as xml, not as a string.
Spoiler


hi.
xml file is somewhere else ,I read it from within the program, Now how do I use the code that you said?
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: question about xml in c#

Posted 27 December 2011 - 08:43 AM

You don't use the code he showed, it's in VB.NET, and in this case, there's no C# analog to that.

@the other posters,
You don't need to use Regex to deal with XML. It's both overkill and underkill at the same time. C# has two great XML providers (System.Xml and System.Xml.Linq), one based on DOM, the other based on Collections. Use them.

Anyway, I can't understand the OP's question, so I don't know how to specifically help him out, but generally I'd tell you to look into the System.Xml.Linq namespace, the XElement class in general.
Was This Post Helpful? 0
  • +
  • -

#10 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: question about xml in c#

Posted 27 December 2011 - 08:51 AM

Yeah, AdamSpeight2008 was totally right on the

Quote

It's xml treat as xml, not as a string.
part. If it's XML then you should use classes that .NET provides for XML.

And a clarification of the OP's question:

He needs to delete some XML tags. :D

And in this case I think that the System.Xml suits this situation better than System.Xml.Linq.
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: question about xml in c#

Posted 27 December 2011 - 08:55 AM

Quote

And in this case I think that the System.Xml suits this situation better than System.Xml.Linq.


What exactly do you base that on?
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: question about xml in c#

Posted 27 December 2011 - 08:57 AM

Well the XML document is referring to a namespace schema that does not exist at the location specified. Appears to be something related to an ECE Protocol which in turn appears to be something uniquely Iranian?
Was This Post Helpful? 0
  • +
  • -

#13 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: question about xml in c#

Posted 27 December 2011 - 09:04 AM

When I say System.Xml I think of both System.Xml AND System.Xml.Linq, since System.Xml.Linq belongs in the Sytem.Xml.

Meaning of what I just wrote:

When I say system.xml I think of the both namespaces, and since system.xml is more generalized and can do more than system.xml.linq, that's why it's better.

This post has been edited by RexGrammer: 27 December 2011 - 09:05 AM

Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: question about xml in c#

Posted 27 December 2011 - 09:24 AM

Well, not to get sidetracked here, but you're only half right. You could technically refer to sub-namespaces as being owned by their parent namespaces, but in practical use, they're segmented; separate entities. In fact, if they had been created at the same time, they'd likely have been given equal "levels". The difference is, LINQ came later, so they extended an older, existing namespace.

Practically, you don't mix and match. You either use the tools from System.Xml, like XmlDocument and XmlElement, or you use the tools from System.Xml.Linq, like XDocument and XElement. The components in the namespaces work well together, in fact were made to work together. Not made to interoperate with the things in the other namespaces.

So, really, claiming that one is better than the other because it includes the other is only technically correct, not practically correct.
Was This Post Helpful? 2
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: question about xml in c#

Posted 27 December 2011 - 11:36 AM

View Postmalikahaki, on 27 December 2011 - 07:09 AM, said:

hi.
xml file is somewhere else ,I read it from within the program, Now how do I use the code that you said?


The xml could easily come from being loaded in from a file, I embedded it to make it easier to test. The important part is the section underneath that builds the new version, which then could be saved out to file. The xml literals are converted to XElements, so it can be done in C#, they just a pain to work it and long winded. It just
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1