Modify an X12

Feb 6 at 9:10 PM
Is there a way for me to load an X12 into an Interchange Object and then modify the contents via the Object rather than serializing it as XML and then converting it back to X12?
Coordinator
Feb 6 at 9:28 PM
You can traverse to the Segment you want to change and call SetElement to make changes to existing segments.
You can also call AddLoop on any LoopContainer or AddSegment on any Container.

When you are done you call ToX12String on the Interchange to serialize it back to X12.
Marked as answer by isaackleinman on 2/6/2014 at 2:37 PM
Feb 7 at 1:36 PM
Is there a way to use xpath-style syntax? Currently my statements look something like this:
 interchange.FunctionGroups.First().Transactions.First().
                  HLoops.First().HLoops.First().HLoops.First().
                  Segments.ElementAt(1).SetElement(9, cin);
Coordinator
Feb 7 at 2:22 PM
If you want to use XPATH like syntax. You can serialize to XML, load it into an XmlDocument, make your changes. Then use the SerializeToX12 method to change the xml back to X12. The code snippet below is from the ParseAndTransformToX12 unit test that demonstrates that they should produce the exact same x12 as the original. You would just need to do your transformation to the xml variable in between.
This works best if you are just changing existing elements. If you are adding and removing elements it is better to use the object model because some validation against the transaction set's specification will occur.
                var parser = new X12Parser();
                Interchange interchange = parser.ParseMultiple(stream).First();
                string originalX12 = interchange.SerializeToX12(true);

                string xml = interchange.Serialize();
                string x12 = parser.TransformToX12(xml);

                Interchange newInterchange = parser.ParseMultiple(x12).First();
                string newX12 = newInterchange.SerializeToX12(true);

                Assert.AreEqual(originalX12, newX12);
Feb 10 at 8:46 PM
The transaction type I'm currently working with is a 271. Since the parser clearly has the specification for this, shouldn't I be able to construct something like a '271Transaction' type and then access, say, the subsciber name like this: 271Transaction.subscriber rather than via Xpath?